package com.zhiyou.app.shop.website.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.DateUtil;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.IDicItemDao;
import com.zhiyou.app.shop.website.dao.IMoneyDealDao;
import com.zhiyou.app.shop.website.dao.IPrizePayDao;
import com.zhiyou.app.shop.website.dao.IUserAccountDao;
import com.zhiyou.app.shop.website.dao.IUserBankDao;
import com.zhiyou.app.shop.website.dao.IUserInfoDao;
import com.zhiyou.app.shop.website.dao.pojo.ActivityApply;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.MoneyDeal;
import com.zhiyou.app.shop.website.dao.pojo.PrizePay;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.dao.pojo.UserBank;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.IUserInfoService;
import com.zhiyou.app.shop.website.service.IUserManagerService;
import com.zhiyou.app.shop.website.service.IUserStockService;

@Service(value="userManagerService")
public class UserManagerServiceImpl implements IUserManagerService {
	
	@Autowired
	private IUserInfoDao userInfoDao;
	@Autowired
	private IUserBankDao userBankDao;
	@Autowired
	private IUserAccountDao userAccountDao;
	@Autowired
	private IMoneyDealDao moneyDealDao;
	@Autowired
	private IDicItemDao dicItemDao;
	@Autowired
	private IDicItemService dicItemService;
	@Autowired
	private IPrizePayDao prizePayDao;
	@Autowired
	private IUserInfoService userInfoService;
	@Autowired
	private IUserStockService userStockService;
	
	public List<User> login(User user) {
		return userInfoDao.findByExample(user);
		/*DetachedCriteria carteria = DetachedCriteria.forClass(User.class);
		carteria.add(Expression.eq("userAccount", user.getUserAccount()));
		userInfoDao.findByCriteria(dCri, order)*/
		//return null;
		
		
	}

	
	public List<User> getUser(String userAccount) {
		User user = new User();
		user.setUserAccount(userAccount);
		user.setFlag(ShopConstants.TRUE);
		user.setActivity(ShopConstants.TRUE);
		return userInfoDao.findByExample(user);
	}
	
	public List<User> getUser2(String userAccount) {
		User user = new User();
		user.setUserAccount(userAccount);
		return userInfoDao.findByExample(user);
	}
	
	public String getGeneralAccount(){
		//1000000---9999999
		//0-8999999
		Random rand = new Random();
		boolean isAccount = true;
		String randStr = "";
		do {
			randStr = 1000000 + rand.nextInt(8999999)+"";
			List<User> list = getUser2("MY"+randStr);
			if(list.isEmpty())
				isAccount = false;
		} while (isAccount);
		return "MY"+randStr;
	}

	public List<User> checkParentAndLoc(String parentId, String location) {
		User user = new User();
		user.setParentId(parentId);
		user.setLocation(location);
		user.setFlag(ShopConstants.TRUE);
		return  userInfoDao.findByExample(user);
	}

	public List<UserBank> getUserBankByUid(String userId) {
		UserBank userBank = new UserBank();
		userBank.setUserId(userId);
		return userBankDao.findByExample(userBank);
	}

	public List<UserBank> getUserBankByAccount(String userAccount) {
		return userBankDao.findByProperty("userId", userAccount);
		//return userBankDao.findUserBankByAccount(userAccount);
	}

	/**
	 * 保存用户信息和银行信息
	 */
	public void saveUserAndBank(User user, UserBank userBank, String type) {
		if("myself".equals(type)){ //加单 扣除加单币   并记流水账
			
			
			
		}
		userInfoDao.save(user);
		userBank.setUserId(user.getUserAccount());
		userBankDao.save(userBank);
	}

	/**
	 * 被同一推荐人推荐的用户列表
	 */
	public void getCheckUser(Page page, String recommend) {
		User user = new User();
		user.setRecommendId(recommend);
		userInfoDao.findByExample(page, user, Order.desc("registerDate"));
				//Property("recommendId", recommend, Order.desc("registerDate"));
	}

	/**
	 * 升级会员级别并扣除相应报单币 ，并做流水记录
	 * @return 0:成功   1：报单币不足 升级失败    2：操作失败，重试
	 */
	public int promote(String userId, String level) {
		User user = getUserById(userId);
		if(user != null){
			//BigDecimal oldLevel = new BigDecimal(user.getLevel());
			//BigDecimal needMoney = new BigDecimal(0);
			BigDecimal oldMoney = new BigDecimal(dicItemService.findByTypeAndCode(ShopConstants.LEVEL, user.getLevel()).getAttr1());
			DicItem levelItem = dicItemService.findByTypeAndCode(ShopConstants.LEVEL, level);
			BigDecimal newMoney = new BigDecimal(levelItem.getAttr1());
			/*List<DicItem> dicItems = dicItemDao.findByProperty("type", ShopConstants.LEVEL, Order.asc("code"));
			for (int i = new Integer(user.getLevel()); i < new Integer(level); i++) {
				System.out.println(dicItems.get(i).getAttr1());
				needMoney = needMoney.add(new BigDecimal(dicItems.get(i).getAttr1()));
			}*/
			BigDecimal needMoney = newMoney.subtract(oldMoney);  //升级所用的报单币
			List<UserAccount> listUserAccount = userAccountDao.findByProperty("userId", user.getUserAccount());
			if(listUserAccount != null && listUserAccount.size() > 0){
				UserAccount userAccount = listUserAccount.get(0);
				BigDecimal activity = userAccount.getActivMoney();
				BigDecimal activityBalance = activity.subtract(needMoney);
				if(activityBalance.compareTo(BigDecimal.ZERO) ==0 || activityBalance.compareTo(BigDecimal.ZERO) ==1){//大于等于
					Date datenow = new Date();
					Timestamp time = new Timestamp(datenow.getTime());
					String date = DateUtil.convertDateToString(datenow);
					user.setLevel(level);
					userAccount.setActivMoney(activityBalance);
					MoneyDeal moneyDeal = new MoneyDeal();
					moneyDeal.setUserId(userId);
					moneyDeal.setAccountSubject("会员升级");
					moneyDeal.setAccountType(ShopConstants.ACTIVITY);
					moneyDeal.setPay(needMoney);
					moneyDeal.setCreateDate(time);
					moneyDeal.setSubjectType("6");
					moneyDeal.setRemark("会员升级扣除激活豆");
					userAccountDao.update(userAccount);
					moneyDealDao.save(moneyDeal);
					userInfoDao.update(user);
					////////////begin 奖金发放扣除 ////////////
					//推荐人
					List<User> recommendList = getUser(user.getRecommendId());
					User recommendUser = null;
					if(recommendList != null && recommendList.size() > 0){
						recommendUser = recommendList.get(0);
					}
					UserAccount recommendAccount = null;
					List<UserAccount> accountList = userAccountDao.findByProperty("userId", user.getRecommendId());//userId  ----account
					recommendAccount = accountList.get(0);
					//会员初始化分配金额  一星600-40%  二星3000-42%  三星6000-45% 四星12000-47% 五星30000-50%
					BigDecimal userStock = needMoney.multiply(new BigDecimal(levelItem.getAttr2()));
					MoneyDeal dealup = new MoneyDeal();
					List<DicItem> stockFlagList = dicItemService.findByType(ShopConstants.STOCK_FLAG);
					DicItem stockFlag = new DicItem();
					if(stockFlagList != null && stockFlagList.size() >0){
						stockFlag = stockFlagList.get(0);
					}
					if("0".equals(stockFlag.getCode())){  //股票停盘中     不买股票 生成股金
						if(userAccount.getStockMoney() != null){
							userAccount.setStockMoney(userAccount.getStockMoney().add(userStock));  //游戏豆							
						}else{
							userAccount.setStockMoney(userStock);
						}
						dealup.setIncome(userStock);
						dealup.setAccountType(ShopConstants.STOCK);
						
					}else if("1".equals(stockFlag.getCode())){  //买成股票没有股金了
						if(userAccount.getStockNum()  != null){
							userAccount.setStockNum(userAccount.getStockNum()+(int)userStockService.buyStock(user.getUserAccount(),userStock.floatValue()));  //股票	
						}
						dealup.setIncome(new BigDecimal(userStockService.buyStock(user.getUserId(),userStock.floatValue())));
						dealup.setAccountType(ShopConstants.STOCK_NUM);
					}
					//TODO
					/*userAccount.setStockNum(0); //股票数量
					userAccount.setStockSaleNum(0);
					userAccount.setStockBuyNum(0);*/
					userAccountDao.save(userAccount);
					
					dealup.setAccountSubject("会员升级分配");
					dealup.setCreateDate(time);
					dealup.setUserId(user.getUserId());
					dealup.setSubjectType("6");
					dealup.setRemark("会员升级");
					moneyDealDao.save(dealup);
					//推荐人的级别数据字典
					DicItem recommendItem = dicItemService.findByTypeAndCode(ShopConstants.LEVEL, recommendUser.getLevel());
					//直推奖
					BigDecimal directPrize = new BigDecimal(recommendItem.getAttr3()).multiply(needMoney);
					DicItem prize1 = dicItemService.findByTypeAndCode(ShopConstants.PRIZE_SCALE, "1");
					DicItem prize2 = dicItemService.findByTypeAndCode(ShopConstants.PRIZE_SCALE, "2");
					DicItem prize3 = dicItemService.findByTypeAndCode(ShopConstants.PRIZE_SCALE, "3");
					BigDecimal prizeRate = new BigDecimal(prize1.getAttr1());  //0.88
					BigDecimal consumeRate = new BigDecimal(prize2.getAttr1()); //0.1
					BigDecimal loveRate = new BigDecimal(prize3.getAttr1());  //0.02
					
					BigDecimal directPrizet = directPrize.multiply(prizeRate);
					BigDecimal directPrizea = directPrize.multiply(consumeRate);
					BigDecimal directPrizel =  directPrize.multiply(loveRate);
					recommendAccount.setBonusMoney(recommendAccount.getBonusMoney().add(directPrizet));
					recommendAccount.setConsumerMoney(recommendAccount.getConsumerMoney().add(directPrizea));
					recommendAccount.setRecommendMoney(recommendAccount.getRecommendMoney().add(directPrizel));
					userAccountDao.save(recommendAccount);
					MoneyDeal recommendmoneyDeal = new MoneyDeal();
					recommendmoneyDeal.setAccountSubject(ShopConstants.DIRECT);
					recommendmoneyDeal.setAccountType(ShopConstants.PRIZE);
					recommendmoneyDeal.setCreateDate(time);
					recommendmoneyDeal.setIncome(directPrizet);
					recommendmoneyDeal.setUserId(recommendUser.getUserId());
					recommendmoneyDeal.setSubjectType("1");
					recommendmoneyDeal.setRemark(user.getUserAccount());
					recommendmoneyDeal.setDealId(user.getUserAccount());
					moneyDealDao.save(recommendmoneyDeal);
					//
					PrizePay pay = new PrizePay();
					pay.setCreateDate(time);
					pay.setPushPrize(directPrizet);
					pay.setConsumerPrize(directPrizea);
					pay.setRegisterAmount(needMoney);
					pay.setRelationUser(user.getUserAccount());
					pay.setUserId(recommendUser.getUserAccount());
					pay.setLoveFund(directPrizel);
					prizePayDao.save(pay);
					//重消
					MoneyDeal recommendmoneyDeal2 = new MoneyDeal();
					recommendmoneyDeal2.setAccountSubject(ShopConstants.RECONSUME);
					recommendmoneyDeal2.setAccountType(ShopConstants.RESUMER);
					recommendmoneyDeal2.setCreateDate(time);
					recommendmoneyDeal2.setIncome(directPrizea);
					recommendmoneyDeal2.setUserId(recommendUser.getUserId());
					recommendmoneyDeal2.setSubjectType("1");
					recommendmoneyDeal2.setDealId(user.getUserAccount());
					recommendmoneyDeal2.setRemark(user.getUserAccount());
					moneyDealDao.save(recommendmoneyDeal2);
					//爱心基金
					MoneyDeal recommendmoneyDeal3 = new MoneyDeal();
					recommendmoneyDeal3.setAccountSubject(ShopConstants.LOVE);
					recommendmoneyDeal3.setAccountType(ShopConstants.LOVE_FUND);
					recommendmoneyDeal3.setCreateDate(time);
					recommendmoneyDeal3.setIncome(directPrizel);
					recommendmoneyDeal3.setUserId(recommendUser.getUserId());
					recommendmoneyDeal3.setSubjectType("1");
					recommendmoneyDeal3.setDealId(user.getUserAccount());
					recommendmoneyDeal3.setRemark(user.getUserAccount());
					moneyDealDao.save(recommendmoneyDeal2);
					
					//对碰奖金率
					DicItem bounsItem = new DicItem();
					bounsItem.setType(ShopConstants.BONUS);
					List<DicItem> bonusList = dicItemDao.findByExample(bounsItem);
					BigDecimal bonusRate = null;
					if(bonusList != null && bonusList.size() > 0){
						bonusRate = new BigDecimal(bonusList.get(0).getCode());
					}
					//对碰奖 领导奖  见点奖
					List<User> parentList = new ArrayList<User>();
					findParent(parentList, user);//链接父节点
					String location = user.getLocation();
					Integer leftleave = null;
					BigDecimal leftleaveAmount = null;
					Integer rightleave = null;
					BigDecimal rightleaveAmount = null;
					BigDecimal levelamount = needMoney;
					BigDecimal bonus = null; //对碰奖金
					MoneyDeal deal = null;
					MoneyDeal deal2 = null;
					MoneyDeal deal3 = null;
					UserAccount parentAccount = null;
					Integer topBonus = 0;
					BigDecimal totalBounsDay = BigDecimal.ZERO;
					int floor = 0;
					int billCnt = (needMoney.intValue()/600);
					User leader1 = null;
					User leader2 = null;
					User leader3 = null;
					UserAccount leaderAccount1 = null;
					UserAccount leaderAccount2 = null;
					UserAccount leaderAccount3 = null;
					BigDecimal leadprize = BigDecimal.ZERO;
					for (User parent : parentList) {
						if("1".equals(location)){  //1:左区          2:右区
							parent.setSparel(parent.getSparel() + billCnt);
							parent.setSparelAmount(parent.getSparelAmount().add(levelamount));
							leftleave = parent.getLeftLeave() + billCnt;  //现有的
							leftleaveAmount = parent.getLeftLeaveAmount().add(levelamount); //现有的     >0
							rightleave = parent.getRightLeave();
							rightleaveAmount = parent.getRightLeaveAmount();
							if(leftleaveAmount.compareTo(rightleaveAmount)== 0){ //相等						
								//全部对碰
								bonus = leftleaveAmount.multiply(bonusRate);  //对碰奖金
								parent.setLeftLeave(0);
								parent.setLeftLeaveAmount(BigDecimal.ZERO);
								parent.setRightLeave(0);
								parent.setRightLeaveAmount(BigDecimal.ZERO);
								userInfoDao.update(parent);
							}else if(leftleaveAmount.compareTo(rightleaveAmount)== -1){ //小于   左边小于右边   把左边对碰掉
								bonus = leftleaveAmount.multiply(bonusRate);//对碰奖金
								parent.setLeftLeave(0);
								parent.setLeftLeaveAmount(BigDecimal.ZERO);
								parent.setRightLeave(parent.getRightLeave()-billCnt);
								parent.setRightLeaveAmount(rightleaveAmount.subtract(leftleaveAmount)); //右边减去左边 剩余的
								userInfoDao.update(parent);
								
							}else if(leftleaveAmount.compareTo(rightleaveAmount)== 1){ //大于  左边大于右边
								if(rightleaveAmount.compareTo(BigDecimal.ZERO) == 0){//相等0
									parent.setLeftLeave(leftleave);
									parent.setLeftLeaveAmount(leftleaveAmount);
									userInfoDao.update(parent);
									//没有对碰奖
									bonus = null;
								}else if(rightleaveAmount.compareTo(BigDecimal.ZERO) == 1){//大于0
									bonus = rightleaveAmount.multiply(bonusRate); //对碰奖金
									parent.setLeftLeave(leftleave-billCnt);
									parent.setLeftLeaveAmount(leftleaveAmount.subtract(rightleaveAmount));
									parent.setRightLeave(0);
									parent.setRightLeaveAmount(BigDecimal.ZERO);
									userInfoDao.update(parent);
								}	
							}
						}else{ // 2 右区
							parent.setSparer(parent.getSparer()+1);
							parent.setApaerAmount(parent.getApaerAmount().add(levelamount));
							leftleave = parent.getLeftLeave();  //现有的
							leftleaveAmount = parent.getLeftLeaveAmount(); //现有的
							rightleave = parent.getRightLeave()+billCnt;
							rightleaveAmount = parent.getRightLeaveAmount().add(levelamount);  //右边大于0
							if(leftleaveAmount.compareTo(rightleaveAmount)== 0){ //相等
								//全部对碰
								bonus = leftleaveAmount.multiply(bonusRate);  //对碰奖金
								parent.setLeftLeave(0);
								parent.setLeftLeaveAmount(BigDecimal.ZERO);
								parent.setRightLeave(0);
								parent.setRightLeaveAmount(BigDecimal.ZERO);
								userInfoDao.update(parent);
							}else if(leftleaveAmount.compareTo(rightleaveAmount)== 1){//左边大于右边
								//对碰掉右边
								bonus = rightleaveAmount.multiply(bonusRate);  //对碰奖金
								parent.setLeftLeave(leftleave-billCnt);
								parent.setLeftLeaveAmount(leftleaveAmount.subtract(rightleaveAmount));
								parent.setRightLeave(0);
								parent.setRightLeaveAmount(BigDecimal.ZERO);
								userInfoDao.update(parent);
								
							}else if(leftleaveAmount.compareTo(rightleaveAmount)== -1){//右边大于左边
								if(leftleaveAmount.compareTo(BigDecimal.ZERO) == 0){
									parent.setRightLeave(rightleave);
									parent.setRightLeaveAmount(rightleaveAmount);
									userInfoDao.update(parent);
									//没有对碰奖
									bonus = null;
								}else if(leftleaveAmount.compareTo(BigDecimal.ZERO) == 1){//左边大于0  对碰掉左边的
									bonus = leftleaveAmount.multiply(bonusRate);
									parent.setLeftLeave(0);
									parent.setLeftLeaveAmount(BigDecimal.ZERO);
									parent.setRightLeave(rightleave-billCnt);
									parent.setRightLeaveAmount(rightleaveAmount.subtract(leftleaveAmount));
									userInfoDao.update(parent);
									//parent.
								}
							}
						}
						if(bonus != null){
							
							//重消奖  奖金的10%   那么实际的奖金是90%
							
							//计算奖金封顶值   查询出当前一天的对碰奖总数
							//根据会员级别，得到封顶值，取出当天总共的对碰奖金  与封顶值比较若超出则扣掉超出的那部分
							//封顶值
							topBonus = dicItemService.findByTypeAndCode(ShopConstants.LEVEL, parent.getLevel()).getSeg1();
							//今天的总对碰奖
							totalBounsDay = moneyDealDao.findBonusByDay(parent.getUserId(), date);
							if(totalBounsDay.compareTo(new BigDecimal(topBonus)) == 0){//相等  已经封顶了 不在加奖
								
							}else{  //未到封顶值时
								totalBounsDay = totalBounsDay.add(bonus.multiply(prizeRate));  //加上本次的
								if(totalBounsDay.compareTo(new BigDecimal(topBonus)) == 0 || totalBounsDay.compareTo(new BigDecimal(topBonus)) == -1){ //恰好到封顶值
									deal = new MoneyDeal();
									deal.setAccountSubject(ShopConstants.BONUSS);
									deal.setAccountType(ShopConstants.PRIZE);
									deal.setCreateDate(time);
									deal.setIncome(bonus.multiply(prizeRate));
									deal.setSubjectType("1");
									deal.setUserId(parent.getUserId());
									deal.setRemark(user.getUserAccount());
									deal.setDealId(user.getUserAccount());
									moneyDealDao.save(deal);	
									deal2 = new MoneyDeal();
									deal2.setAccountSubject(ShopConstants.RECONSUME);
									deal2.setAccountType(ShopConstants.RESUMER);
									deal2.setCreateDate(time);
									deal2.setIncome(bonus.multiply(consumeRate));
									deal2.setSubjectType("1");
									deal2.setUserId(parent.getUserId());
									deal2.setRemark(user.getUserAccount());
									//deal.setDealId(user.getUserAccount());
									moneyDealDao.save(deal2);
									
									//爱心基金
									deal3 = new MoneyDeal();
									deal3.setAccountSubject(ShopConstants.LOVE);
									deal3.setAccountType(ShopConstants.LOVE_FUND);
									deal3.setCreateDate(time);
									deal3.setIncome(bonus.multiply(loveRate));
									deal3.setUserId(parent.getUserId());
									deal3.setSubjectType("1");
									//deal3.setDealId(user.getUserAccount());
									deal3.setRemark(user.getUserAccount());
									moneyDealDao.save(deal3);
									
									PrizePay paybouns = new PrizePay();
									paybouns.setCreateDate(time);
									paybouns.setPairsPrize(bonus.multiply(prizeRate));
									paybouns.setConsumerPrize(bonus.multiply(consumeRate));
									paybouns.setRegisterAmount(needMoney);
									paybouns.setRelationUser(user.getUserAccount());
									paybouns.setUserId(parent.getUserAccount());
									paybouns.setLoveFund(bonus.multiply(loveRate));
									prizePayDao.save(paybouns);
									
									//奖金累加到个人账户
									parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
									parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(bonus.multiply(prizeRate)));  //88%的奖金
									parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(bonus.multiply(consumeRate))); //10%的重消
									parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(bonus.multiply(loveRate))); //2%的爱心基金
									userAccountDao.update(parentAccount);	
									
									//领导奖    拿到对碰奖的向上找三代推荐人计算
									//第一代推荐人， 直接推荐人
									leader1 = userInfoService.findByAccount(parent.getRecommendId());
									if(leader1 != null){
										leadprize = bonus.multiply(new BigDecimal(0.1));
										deal = new MoneyDeal();
										deal.setAccountSubject(ShopConstants.LEADER);
										deal.setAccountType(ShopConstants.PRIZE);
										deal.setCreateDate(time);
										deal.setIncome(leadprize.multiply(prizeRate));
										deal.setSubjectType("1");
										deal.setUserId(leader1.getUserId());
										deal.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal);	
										deal2 = new MoneyDeal();
										deal2.setAccountSubject(ShopConstants.RECONSUME);
										deal2.setAccountType(ShopConstants.RESUMER);
										deal2.setCreateDate(time);
										deal2.setIncome(leadprize.multiply(consumeRate));
										deal2.setSubjectType("1");
										deal2.setUserId(leader1.getUserId());
										deal2.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal2);
										
										//爱心基金
										deal3 = new MoneyDeal();
										deal3.setAccountSubject(ShopConstants.LOVE);
										deal3.setAccountType(ShopConstants.LOVE_FUND);
										deal3.setCreateDate(time);
										deal3.setIncome(leadprize.multiply(loveRate));
										deal3.setUserId(leader1.getUserId());
										deal3.setSubjectType("1");
										//deal3.setDealId(user.getUserAccount());
										deal3.setRemark(parent.getUserAccount());
										moneyDealDao.save(deal3);
										
										PrizePay paybounsl = new PrizePay();
										paybounsl.setCreateDate(time);
										paybounsl.setLeaderPrize(leadprize.multiply(prizeRate));
										paybounsl.setConsumerPrize(leadprize.multiply(consumeRate));
										paybounsl.setRegisterAmount(needMoney);
										paybounsl.setRelationUser(user.getUserAccount());
										paybounsl.setUserId(leader1.getUserAccount());
										paybounsl.setLoveFund(leadprize.multiply(loveRate));
										prizePayDao.save(paybounsl);
										leaderAccount1 = userAccountDao.findByAccount(leader1.getUserAccount());
										leaderAccount1.setBonusMoney(leaderAccount1.getBonusMoney().add(leadprize.multiply(prizeRate)));
										leaderAccount1.setConsumerMoney(leaderAccount1.getConsumerMoney().add(leadprize.multiply(consumeRate)));
										leaderAccount1.setRecommendMoney(leaderAccount1.getRecommendMoney().add(leadprize.multiply(loveRate)));
										userAccountDao.update(leaderAccount1);
										//第二代
										leader2 = userInfoService.findByAccount(leader1.getRecommendId());
										if(leader2 != null){
											if("2".equals(leader2.getLevel())){  //二星  二代的5%
												leadprize = bonus.multiply(new BigDecimal(0.05));
												deal = new MoneyDeal();
												deal.setAccountSubject(ShopConstants.LEADER);
												deal.setAccountType(ShopConstants.PRIZE);
												deal.setCreateDate(time);
												deal.setIncome(leadprize.multiply(prizeRate));
												deal.setSubjectType("1");
												deal.setUserId(leader2.getUserId());
												deal.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal);	
												deal2 = new MoneyDeal();
												deal2.setAccountSubject(ShopConstants.RECONSUME);
												deal2.setAccountType(ShopConstants.RESUMER);
												deal2.setCreateDate(time);
												deal2.setIncome(leadprize.multiply(consumeRate));
												deal2.setSubjectType("1");
												deal2.setUserId(leader2.getUserId());
												deal2.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal2);
												
												//爱心基金
												deal3 = new MoneyDeal();
												deal3.setAccountSubject(ShopConstants.LOVE);
												deal3.setAccountType(ShopConstants.LOVE_FUND);
												deal3.setCreateDate(time);
												deal3.setIncome(leadprize.multiply(loveRate));
												deal3.setUserId(leader2.getUserId());
												deal3.setSubjectType("1");
												//deal3.setDealId(user.getUserAccount());
												deal3.setRemark(parent.getUserAccount());
												moneyDealDao.save(deal3);
												
												PrizePay paybouns2 = new PrizePay();
												paybouns2.setCreateDate(time);
												paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
												paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
												paybouns2.setRegisterAmount(needMoney);
												paybouns2.setRelationUser(user.getUserAccount());
												paybouns2.setUserId(leader2.getUserAccount());
												paybouns2.setLoveFund(leadprize.multiply(loveRate));
												prizePayDao.save(paybouns2);
												leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
												leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
												leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
												leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
												userAccountDao.update(leaderAccount2);
											}else if("3".equals(leader2.getLevel()) || "4".equals(leader2.getLevel()) || "5".equals(leader2.getLevel())){ //三星 四星 五星 10%
												leadprize = bonus.multiply(new BigDecimal(0.1));
												deal = new MoneyDeal();
												deal.setAccountSubject(ShopConstants.LEADER);
												deal.setAccountType(ShopConstants.PRIZE);
												deal.setCreateDate(time);
												deal.setIncome(leadprize.multiply(prizeRate));
												deal.setSubjectType("1");
												deal.setUserId(leader2.getUserId());
												deal.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal);	
												deal2 = new MoneyDeal();
												deal2.setAccountSubject(ShopConstants.RECONSUME);
												deal2.setAccountType(ShopConstants.RESUMER);
												deal2.setCreateDate(time);
												deal2.setIncome(leadprize.multiply(consumeRate));
												deal2.setSubjectType("1");
												deal2.setUserId(leader2.getUserId());
												deal2.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal2);
												
												//爱心基金
												deal3 = new MoneyDeal();
												deal3.setAccountSubject(ShopConstants.LOVE);
												deal3.setAccountType(ShopConstants.LOVE_FUND);
												deal3.setCreateDate(time);
												deal3.setIncome(leadprize.multiply(loveRate));
												deal3.setUserId(leader2.getUserId());
												deal3.setSubjectType("1");
												//deal3.setDealId(user.getUserAccount());
												deal3.setRemark(parent.getUserAccount());
												moneyDealDao.save(deal3);
												
												PrizePay paybouns2 = new PrizePay();
												paybouns2.setCreateDate(time);
												paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
												paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
												paybouns2.setRegisterAmount(needMoney);
												paybouns2.setRelationUser(user.getUserAccount());
												paybouns2.setUserId(leader2.getUserAccount());
												paybouns2.setLoveFund(leadprize.multiply(loveRate));
												prizePayDao.save(paybouns2);
												leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
												leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
												leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
												leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
												userAccountDao.update(leaderAccount2);
											}
											//第三代
											leader3 = userInfoService.findByAccount(leader2.getRecommendId());
											if(leader3 != null){
												if("4".equals(leader3.getLevel())){  //四星  三代的5%
													leadprize = bonus.multiply(new BigDecimal(0.05));
													deal = new MoneyDeal();
													deal.setAccountSubject(ShopConstants.LEADER);
													deal.setAccountType(ShopConstants.PRIZE);
													deal.setCreateDate(time);
													deal.setIncome(leadprize.multiply(prizeRate));
													deal.setSubjectType("1");
													deal.setUserId(leader3.getUserId());
													deal.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal);	
													deal2 = new MoneyDeal();
													deal2.setAccountSubject(ShopConstants.RECONSUME);
													deal2.setAccountType(ShopConstants.RESUMER);
													deal2.setCreateDate(time);
													deal2.setIncome(leadprize.multiply(consumeRate));
													deal2.setSubjectType("1");
													deal2.setUserId(leader3.getUserId());
													deal2.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal2);
													
													//爱心基金
													deal3 = new MoneyDeal();
													deal3.setAccountSubject(ShopConstants.LOVE);
													deal3.setAccountType(ShopConstants.LOVE_FUND);
													deal3.setCreateDate(time);
													deal3.setIncome(leadprize.multiply(loveRate));
													deal3.setUserId(leader3.getUserId());
													deal3.setSubjectType("1");
													//deal3.setDealId(user.getUserAccount());
													deal3.setRemark(parent.getUserAccount());
													moneyDealDao.save(deal3);
													
													PrizePay paybouns2 = new PrizePay();
													paybouns2.setCreateDate(time);
													paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
													paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
													paybouns2.setRegisterAmount(needMoney);
													paybouns2.setRelationUser(user.getUserAccount());
													paybouns2.setUserId(leader3.getUserAccount());
													paybouns2.setLoveFund(leadprize.multiply(loveRate));
													prizePayDao.save(paybouns2);
													leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
													leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
													leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
													leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
													userAccountDao.update(leaderAccount3);
												}else if("5".equals(leader3.getLevel())){ //五星 10%
													leadprize = bonus.multiply(new BigDecimal(0.1));
													deal = new MoneyDeal();
													deal.setAccountSubject(ShopConstants.LEADER);
													deal.setAccountType(ShopConstants.PRIZE);
													deal.setCreateDate(time);
													deal.setIncome(leadprize.multiply(prizeRate));
													deal.setSubjectType("1");
													deal.setUserId(leader3.getUserId());
													deal.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal);	
													deal2 = new MoneyDeal();
													deal2.setAccountSubject(ShopConstants.RECONSUME);
													deal2.setAccountType(ShopConstants.RESUMER);
													deal2.setCreateDate(time);
													deal2.setIncome(leadprize.multiply(consumeRate));
													deal2.setSubjectType("1");
													deal2.setUserId(leader3.getUserId());
													deal2.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal2);
													
													//爱心基金
													deal3 = new MoneyDeal();
													deal3.setAccountSubject(ShopConstants.LOVE);
													deal3.setAccountType(ShopConstants.LOVE_FUND);
													deal3.setCreateDate(time);
													deal3.setIncome(leadprize.multiply(loveRate));
													deal3.setUserId(leader3.getUserId());
													deal3.setSubjectType("1");
													//deal3.setDealId(user.getUserAccount());
													deal3.setRemark(parent.getUserAccount());
													moneyDealDao.save(deal3);
													
													PrizePay paybouns2 = new PrizePay();
													paybouns2.setCreateDate(time);
													paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
													paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
													paybouns2.setRegisterAmount(needMoney);
													paybouns2.setRelationUser(user.getUserAccount());
													paybouns2.setUserId(leader3.getUserAccount());
													paybouns2.setLoveFund(leadprize.multiply(loveRate));
													prizePayDao.save(paybouns2);
													leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
													leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
													leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
													leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
													userAccountDao.update(leaderAccount3);
												}
											}
										}
									}
									
									
									
								}else if(totalBounsDay.compareTo(new BigDecimal(topBonus)) == 1){  //超过封顶
									deal = new MoneyDeal();
									deal.setAccountSubject(ShopConstants.BONUSS);
									deal.setAccountType(ShopConstants.PRIZE);
									deal.setCreateDate(time);
									deal.setIncome(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(prizeRate));
									deal.setSubjectType("1");
									deal.setUserId(parent.getUserId());
									deal.setDealId(user.getUserAccount());
									deal.setRemark(user.getUserAccount());
									moneyDealDao.save(deal);	
									deal2 = new MoneyDeal();
									deal2.setAccountSubject(ShopConstants.RECONSUME);
									deal2.setAccountType(ShopConstants.RESUMER);
									deal2.setCreateDate(time);
									deal2.setIncome(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(consumeRate));
									deal2.setSubjectType("1");
									deal2.setUserId(parent.getUserId());
									deal2.setRemark(user.getUserAccount());
									//deal.setDealId(user.getUserAccount());
									moneyDealDao.save(deal2);
									
									//爱心基金
									deal3 = new MoneyDeal();
									deal3.setAccountSubject(ShopConstants.LOVE);
									deal3.setAccountType(ShopConstants.LOVE_FUND);
									deal3.setCreateDate(time);
									deal3.setIncome(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(loveRate));
									deal3.setUserId(recommendUser.getUserId());
									deal3.setSubjectType("1");
									//deal3.setDealId(user.getUserAccount());
									deal3.setRemark(user.getUserAccount());
									moneyDealDao.save(deal3);
									
									PrizePay paybouns = new PrizePay();
									paybouns.setCreateDate(time);
									paybouns.setPairsPrize(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(prizeRate));
									paybouns.setConsumerPrize(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(consumeRate));
									paybouns.setRegisterAmount(needMoney);
									paybouns.setRelationUser(user.getUserAccount());
									paybouns.setUserId(parent.getUserAccount());
									paybouns.setLoveFund(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(loveRate));
									prizePayDao.save(paybouns);
									
									//奖金累加到个人账户
									parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
									parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(prizeRate)));
									parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(consumeRate)));
									parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(loveRate)));
									userAccountDao.update(parentAccount);
									
									//领导奖    拿到对碰奖的向上找三代推荐人计算
									//第一代推荐人， 直接推荐人
									leader1 = userInfoService.findByAccount(parent.getRecommendId());
									if(leader1 != null){
										leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.1));
										deal = new MoneyDeal();
										deal.setAccountSubject(ShopConstants.LEADER);
										deal.setAccountType(ShopConstants.PRIZE);
										deal.setCreateDate(time);
										deal.setIncome(leadprize.multiply(prizeRate));
										deal.setSubjectType("1");
										deal.setUserId(leader1.getUserId());
										deal.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal);	
										deal2 = new MoneyDeal();
										deal2.setAccountSubject(ShopConstants.RECONSUME);
										deal2.setAccountType(ShopConstants.RESUMER);
										deal2.setCreateDate(time);
										deal2.setIncome(leadprize.multiply(consumeRate));
										deal2.setSubjectType("1");
										deal2.setUserId(leader1.getUserId());
										deal2.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal2);
										
										//爱心基金
										deal3 = new MoneyDeal();
										deal3.setAccountSubject(ShopConstants.LOVE);
										deal3.setAccountType(ShopConstants.LOVE_FUND);
										deal3.setCreateDate(time);
										deal3.setIncome(leadprize.multiply(loveRate));
										deal3.setUserId(leader1.getUserId());
										deal3.setSubjectType("1");
										//deal3.setDealId(user.getUserAccount());
										deal3.setRemark(parent.getUserAccount());
										moneyDealDao.save(deal3);
										
										PrizePay paybounsl = new PrizePay();
										paybounsl.setCreateDate(time);
										paybounsl.setLeaderPrize(leadprize.multiply(prizeRate));
										paybounsl.setConsumerPrize(leadprize.multiply(consumeRate));
										paybounsl.setRegisterAmount(needMoney);
										paybounsl.setRelationUser(user.getUserAccount());
										paybounsl.setUserId(leader1.getUserAccount());
										paybounsl.setLoveFund(leadprize.multiply(loveRate));
										prizePayDao.save(paybounsl);
										leaderAccount1 = userAccountDao.findByAccount(leader1.getUserAccount());
										leaderAccount1.setBonusMoney(leaderAccount1.getBonusMoney().add(leadprize.multiply(prizeRate)));
										leaderAccount1.setConsumerMoney(leaderAccount1.getConsumerMoney().add(leadprize.multiply(consumeRate)));
										leaderAccount1.setRecommendMoney(leaderAccount1.getRecommendMoney().add(leadprize.multiply(loveRate)));
										userAccountDao.update(leaderAccount1);
										//第二代
										leader2 = userInfoService.findByAccount(leader1.getRecommendId());
										if(leader2 != null){
											if("2".equals(leader2.getLevel())){  //二星  二代的5%
												leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.05));
												deal = new MoneyDeal();
												deal.setAccountSubject(ShopConstants.LEADER);
												deal.setAccountType(ShopConstants.PRIZE);
												deal.setCreateDate(time);
												deal.setIncome(leadprize.multiply(prizeRate));
												deal.setSubjectType("1");
												deal.setUserId(leader2.getUserId());
												deal.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal);	
												deal2 = new MoneyDeal();
												deal2.setAccountSubject(ShopConstants.RECONSUME);
												deal2.setAccountType(ShopConstants.RESUMER);
												deal2.setCreateDate(time);
												deal2.setIncome(leadprize.multiply(consumeRate));
												deal2.setSubjectType("1");
												deal2.setUserId(leader2.getUserId());
												deal2.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal2);
												
												//爱心基金
												deal3 = new MoneyDeal();
												deal3.setAccountSubject(ShopConstants.LOVE);
												deal3.setAccountType(ShopConstants.LOVE_FUND);
												deal3.setCreateDate(time);
												deal3.setIncome(leadprize.multiply(loveRate));
												deal3.setUserId(leader2.getUserId());
												deal3.setSubjectType("1");
												//deal3.setDealId(user.getUserAccount());
												deal3.setRemark(parent.getUserAccount());
												moneyDealDao.save(deal3);
												
												PrizePay paybouns2 = new PrizePay();
												paybouns2.setCreateDate(time);
												paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
												paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
												paybouns2.setRegisterAmount(needMoney);
												paybouns2.setRelationUser(user.getUserAccount());
												paybouns2.setUserId(leader2.getUserAccount());
												paybouns2.setLoveFund(leadprize.multiply(loveRate));
												prizePayDao.save(paybouns2);
												leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
												leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
												leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
												leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
												userAccountDao.update(leaderAccount2);
											}else if("3".equals(leader2.getLevel()) || "4".equals(leader2.getLevel()) || "5".equals(leader2.getLevel())){ //三星 四星 五星 10%
												leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.1));
												deal = new MoneyDeal();
												deal.setAccountSubject(ShopConstants.LEADER);
												deal.setAccountType(ShopConstants.PRIZE);
												deal.setCreateDate(time);
												deal.setIncome(leadprize.multiply(prizeRate));
												deal.setSubjectType("1");
												deal.setUserId(leader2.getUserId());
												deal.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal);	
												deal2 = new MoneyDeal();
												deal2.setAccountSubject(ShopConstants.RECONSUME);
												deal2.setAccountType(ShopConstants.RESUMER);
												deal2.setCreateDate(time);
												deal2.setIncome(leadprize.multiply(consumeRate));
												deal2.setSubjectType("1");
												deal2.setUserId(leader2.getUserId());
												deal2.setRemark(parent.getUserAccount());
												//deal.setDealId(user.getUserAccount());
												moneyDealDao.save(deal2);
												
												//爱心基金
												deal3 = new MoneyDeal();
												deal3.setAccountSubject(ShopConstants.LOVE);
												deal3.setAccountType(ShopConstants.LOVE_FUND);
												deal3.setCreateDate(time);
												deal3.setIncome(leadprize.multiply(loveRate));
												deal3.setUserId(leader2.getUserId());
												deal3.setSubjectType("1");
												//deal3.setDealId(user.getUserAccount());
												deal3.setRemark(parent.getUserAccount());
												moneyDealDao.save(deal3);
												
												PrizePay paybouns2 = new PrizePay();
												paybouns2.setCreateDate(time);
												paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
												paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
												paybouns2.setRegisterAmount(needMoney);
												paybouns2.setRelationUser(user.getUserAccount());
												paybouns2.setUserId(leader2.getUserAccount());
												paybouns2.setLoveFund(leadprize.multiply(loveRate));
												prizePayDao.save(paybouns2);
												leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
												leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
												leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
												leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
												userAccountDao.update(leaderAccount2);
											}
											//第三代
											leader3 = userInfoService.findByAccount(leader2.getRecommendId());
											if(leader3 != null){
												if("4".equals(leader3.getLevel())){  //四星  三代的5%
													leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.05));
													deal = new MoneyDeal();
													deal.setAccountSubject(ShopConstants.LEADER);
													deal.setAccountType(ShopConstants.PRIZE);
													deal.setCreateDate(time);
													deal.setIncome(leadprize.multiply(prizeRate));
													deal.setSubjectType("1");
													deal.setUserId(leader3.getUserId());
													deal.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal);	
													deal2 = new MoneyDeal();
													deal2.setAccountSubject(ShopConstants.RECONSUME);
													deal2.setAccountType(ShopConstants.RESUMER);
													deal2.setCreateDate(time);
													deal2.setIncome(leadprize.multiply(consumeRate));
													deal2.setSubjectType("1");
													deal2.setUserId(leader3.getUserId());
													deal2.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal2);
													
													//爱心基金
													deal3 = new MoneyDeal();
													deal3.setAccountSubject(ShopConstants.LOVE);
													deal3.setAccountType(ShopConstants.LOVE_FUND);
													deal3.setCreateDate(time);
													deal3.setIncome(leadprize.multiply(loveRate));
													deal3.setUserId(leader3.getUserId());
													deal3.setSubjectType("1");
													//deal3.setDealId(user.getUserAccount());
													deal3.setRemark(parent.getUserAccount());
													moneyDealDao.save(deal3);
													
													PrizePay paybouns2 = new PrizePay();
													paybouns2.setCreateDate(time);
													paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
													paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
													paybouns2.setRegisterAmount(needMoney);
													paybouns2.setRelationUser(user.getUserAccount());
													paybouns2.setUserId(leader3.getUserAccount());
													paybouns2.setLoveFund(leadprize.multiply(loveRate));
													prizePayDao.save(paybouns2);
													leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
													leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
													leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
													leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
													userAccountDao.update(leaderAccount3);
												}else if("5".equals(leader3.getLevel())){ //五星 10%
													leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.1));
													deal = new MoneyDeal();
													deal.setAccountSubject(ShopConstants.LEADER);
													deal.setAccountType(ShopConstants.PRIZE);
													deal.setCreateDate(time);
													deal.setIncome(leadprize.multiply(prizeRate));
													deal.setSubjectType("1");
													deal.setUserId(leader3.getUserId());
													deal.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal);	
													deal2 = new MoneyDeal();
													deal2.setAccountSubject(ShopConstants.RECONSUME);
													deal2.setAccountType(ShopConstants.RESUMER);
													deal2.setCreateDate(time);
													deal2.setIncome(leadprize.multiply(consumeRate));
													deal2.setSubjectType("1");
													deal2.setUserId(leader3.getUserId());
													deal2.setRemark(parent.getUserAccount());
													//deal.setDealId(user.getUserAccount());
													moneyDealDao.save(deal2);
													
													//爱心基金
													deal3 = new MoneyDeal();
													deal3.setAccountSubject(ShopConstants.LOVE);
													deal3.setAccountType(ShopConstants.LOVE_FUND);
													deal3.setCreateDate(time);
													deal3.setIncome(leadprize.multiply(loveRate));
													deal3.setUserId(leader3.getUserId());
													deal3.setSubjectType("1");
													//deal3.setDealId(user.getUserAccount());
													deal3.setRemark(parent.getUserAccount());
													moneyDealDao.save(deal3);
													
													PrizePay paybouns2 = new PrizePay();
													paybouns2.setCreateDate(time);
													paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
													paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
													paybouns2.setRegisterAmount(needMoney);
													paybouns2.setRelationUser(user.getUserAccount());
													paybouns2.setUserId(leader3.getUserAccount());
													paybouns2.setLoveFund(leadprize.multiply(loveRate));
													prizePayDao.save(paybouns2);
													leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
													leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
													leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
													leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
													userAccountDao.update(leaderAccount3);
												}
											}
										}
									}
								}
							
							}				
						}
						location = parent.getLocation();
						
						floor += 1;
						//见点奖
						if("1".equals(parent.getLevel())){  //一星的   见点奖  2层  4层
							if(floor == 2 || floor ==4){
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.POINT);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(parent.getUserId());
								deal.setDealId(user.getUserAccount());
								deal.setRemark(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
								deal2.setSubjectType("1");
								deal2.setUserId(parent.getUserId());
								deal2.setRemark(user.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
								deal3.setUserId(parent.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(user.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybouns = new PrizePay();
								paybouns.setCreateDate(time);
								paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
								paybouns.setRegisterAmount(needMoney);
								paybouns.setRelationUser(user.getUserAccount());
								paybouns.setUserId(parent.getUserAccount());
								paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
								prizePayDao.save(paybouns);
								
								//奖金累加到个人账户
								parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
								parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
								parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate)));
								parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate)));
								userAccountDao.update(parentAccount);
							}
						}else if("2".equals(parent.getLevel())){
							if(floor == 2 || floor ==4 || floor ==6){
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.POINT);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(parent.getUserId());
								deal.setDealId(user.getUserAccount());
								deal.setRemark(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
								deal2.setSubjectType("1");
								deal2.setUserId(parent.getUserId());
								deal2.setRemark(user.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
								deal3.setUserId(parent.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(user.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybouns = new PrizePay();
								paybouns.setCreateDate(time);
								paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
								paybouns.setRegisterAmount(needMoney);
								paybouns.setRelationUser(user.getUserAccount());
								paybouns.setUserId(parent.getUserAccount());
								paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
								prizePayDao.save(paybouns);
								
								//奖金累加到个人账户
								parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
								parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
								parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate))));
								parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate))));
								userAccountDao.update(parentAccount);
							}
						}else if("3".equals(parent.getLevel())){
							if(floor == 2 || floor ==4 || floor ==6 || floor ==8){
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.POINT);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(parent.getUserId());
								deal.setDealId(user.getUserAccount());
								deal.setRemark(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
								deal2.setSubjectType("1");
								deal2.setUserId(parent.getUserId());
								deal2.setRemark(user.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
								deal3.setUserId(parent.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(user.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybouns = new PrizePay();
								paybouns.setCreateDate(time);
								paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
								paybouns.setRegisterAmount(needMoney);
								paybouns.setRelationUser(user.getUserAccount());
								paybouns.setUserId(parent.getUserAccount());
								paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
								prizePayDao.save(paybouns);
								
								//奖金累加到个人账户
								parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
								parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
								parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate))));
								parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate))));
								userAccountDao.update(parentAccount);
							}
						}else if("4".equals(parent.getLevel())){
							if(floor == 2 || floor ==4 || floor ==6 || floor ==8 || floor ==10 || floor ==12){
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.POINT);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(parent.getUserId());
								deal.setDealId(user.getUserAccount());
								deal.setRemark(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
								deal2.setSubjectType("1");
								deal2.setUserId(parent.getUserId());
								deal2.setRemark(user.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
								deal3.setUserId(parent.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(user.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybouns = new PrizePay();
								paybouns.setCreateDate(time);
								paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
								paybouns.setRegisterAmount(needMoney);
								paybouns.setRelationUser(user.getUserAccount());
								paybouns.setUserId(parent.getUserAccount());
								paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
								prizePayDao.save(paybouns);
								
								//奖金累加到个人账户
								parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
								parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
								parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate)));
								parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate)));
								userAccountDao.update(parentAccount);
							}
						}else if("5".equals(parent.getLevel())){
							if(floor == 2 || floor ==4 || floor ==6 || floor ==8 || floor ==10 || floor ==12 || floor ==14 || floor ==16){
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.POINT);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(parent.getUserId());
								deal.setDealId(user.getUserAccount());
								deal.setRemark(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
								deal2.setSubjectType("1");
								deal2.setUserId(parent.getUserId());
								deal2.setRemark(user.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
								deal3.setUserId(parent.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(user.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybouns = new PrizePay();
								paybouns.setCreateDate(time);
								paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
								paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
								paybouns.setRegisterAmount(needMoney);
								paybouns.setRelationUser(user.getUserAccount());
								paybouns.setUserId(parent.getUserAccount());
								paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
								prizePayDao.save(paybouns);
								
								//奖金累加到个人账户
								parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
								parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
								parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate)));
								parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate)));
								userAccountDao.update(parentAccount);
							}
						}
					}
					
					////////// end  奖金发放扣除/////////////
					return 0;
				}else{
					return 1;
				}
			}
		}
		return 2;
	}

	public User getUserById(String userId) {
		/*List<User> listUser = userInfoDao.findByProperty("userId", userId);
		if(listUser != null && listUser.size() > 0){
			return listUser.get(0);
		}
		return null;*/
		return userInfoDao.findById(userId);
	}

	public void saveUser(User user) {
		userInfoDao.update(user);
	}

	public void saveUserBank(UserBank userBank) {
		userBankDao.update(userBank);
		
	}

	public List<User> getVvUser(String account) {
		User user = new User();
		user.setMainAccount(account);
		user.setFlag(ShopConstants.TRUE);
		return userInfoDao.findByExample(user);
	}

	public BigDecimal getVvPrize(List<User> listUser) {
		BigDecimal vv = new BigDecimal(0);
		for (User user : listUser) {
			List<UserAccount> listAccount = userAccountDao.findByProperty("userId", user.getUserId());
			if(listAccount != null && listAccount.size() > 0){
				vv = vv.add(listAccount.get(0).getBonusMoney());
				
			}
		}
		return vv;
	}

	public List<User> getRecommend(String account) {
		return userInfoDao.findByProperty("recommendId", account);
	}

	/**
	 * @return  0 ：成功               1：激活豆不足，不能激活  2：vv豆不足，不能加单
	 */
	public int auditUser(String curUserId, String userId) {
		Date datenow = new Date();
		Timestamp time = new Timestamp(datenow.getTime());
		String date = DateUtil.convertDateToString(datenow);
		//Long time = new Date().getTime();
		User user = getUserById(userId);
		DicItem dic = new DicItem();
		dic.setType(ShopConstants.LEVEL);
		dic.setCode(user.getLevel());
		List<DicItem> levelList = dicItemDao.findByExample(dic);
		DicItem dicItem = null;
		if(levelList != null && levelList.size() > 0){
			dicItem = levelList.get(0);
		}
		List<User> recommendList = getUser(user.getRecommendId());
		User recommendUser = null;
		if(recommendList != null && recommendList.size() > 0){
			recommendUser = recommendList.get(0);
		}
		UserAccount recommendAccount = null;
		List<UserAccount> accountList = userAccountDao.findByProperty("userId", curUserId);//userId  ----account
		recommendAccount = accountList.get(0);
		if("1".equals(user.getEnterType())){//报单  扣激活币
			if(recommendAccount.getActivMoney().compareTo(new BigDecimal(dicItem.getAttr1())) == -1){
				return 1;
			}else{ //扣除
				recommendAccount.setActivMoney(recommendAccount.getActivMoney().subtract(new BigDecimal(dicItem.getAttr1())));
				MoneyDeal moneyDeal = new MoneyDeal();
				moneyDeal.setAccountSubject("报单");
				moneyDeal.setAccountType(ShopConstants.ACTIVITY);
				moneyDeal.setCreateDate(time);
				moneyDeal.setPay(new BigDecimal(dicItem.getAttr1()));
				moneyDeal.setUserId(recommendUser.getUserId());
				moneyDeal.setSubjectType("6");
				moneyDeal.setRemark(user.getUserAccount());
				moneyDealDao.save(moneyDeal);
			}
		}else if("2".equals(user.getEnterType())){ //加单 扣加单币   vv豆
			if(recommendAccount.getAdditionMoney().compareTo(new BigDecimal(dicItem.getAttr1())) == -1){
				return 2;
			}else{ //扣除
				recommendAccount.setAdditionMoney(recommendAccount.getAdditionMoney().subtract(new BigDecimal(dicItem.getAttr1())));
				MoneyDeal moneyDeal = new MoneyDeal();
				moneyDeal.setAccountSubject("追加户口");
				moneyDeal.setAccountType(ShopConstants.ADD);
				moneyDeal.setCreateDate(time);
				moneyDeal.setPay(new BigDecimal(dicItem.getAttr1()));
				moneyDeal.setUserId(recommendUser.getUserId());
				moneyDeal.setSubjectType("6");
				moneyDeal.setRemark(user.getUserAccount());
				moneyDealDao.save(moneyDeal);
			}
		}
			//会员初始化分配金额  一星600-40%  二星3000-42%  三星6000-45% 四星12000-47% 五星30000-50%
			BigDecimal userStock = new BigDecimal(dicItem.getAttr1()).multiply(new BigDecimal(dicItem.getAttr2()));
					
			
			//激活用户的 初始分配金额和帐目记录
			MoneyDeal moneyDeal = new MoneyDeal();
			UserAccount userAccount = new UserAccount();
			userAccount.setUserId(user.getUserAccount());
			List<DicItem> stockFlagList = dicItemService.findByType(ShopConstants.STOCK_FLAG);
			DicItem stockFlag = new DicItem();
			if(stockFlagList != null && stockFlagList.size() >0){
				stockFlag = stockFlagList.get(0);
			}
			if("0".equals(stockFlag.getCode())){  //股票停盘中     不买股票 生成股金
				userAccount.setStockMoney(userStock);  //游戏豆
				userAccount.setStockNum(0);
				moneyDeal.setIncome(userStock);
				moneyDeal.setAccountType(ShopConstants.STOCK);
				
			}else if("1".equals(stockFlag.getCode())){  //买成股票没有股金了
				userAccount.setStockNum((int)userStockService.buyStock(user.getUserAccount(),userStock.floatValue()));  ////股票数量
				userAccount.setStockMoney(BigDecimal.ZERO);
				moneyDeal.setIncome(new BigDecimal(userStockService.buyStock(user.getUserId(),userStock.floatValue())));
				moneyDeal.setAccountType(ShopConstants.STOCK_NUM);
				
			}
			//userAccount.setStockMoney(userStock);  //游戏豆
			userAccount.setAdditionMoney(new BigDecimal(0));  //加单  VV豆
			userAccount.setActivMoney(new BigDecimal(0)); //激活豆
			userAccount.setBonusMoney(new BigDecimal(0)); //奖金豆
			userAccount.setConsumerMoney(new BigDecimal(0)); //重消豆
			userAccount.setLotteryBalance(new BigDecimal(0)); //彩票费
			//userAccount.setStockMoney(new BigDecimal(0));
			//userAccount.setStockNum(0); //股票数量
			userAccount.setStockSaleNum(0);
			userAccount.setStockBuyNum(0);
			userAccount.setRecommendMoney(new BigDecimal(0));  //爱心基金
			userAccountDao.save(userAccount);
			moneyDeal.setAccountSubject("会员初始分配");
			moneyDeal.setAccountType(ShopConstants.STOCK);
			moneyDeal.setCreateDate(time);
			moneyDeal.setIncome(userStock);
			moneyDeal.setUserId(user.getUserId());
			moneyDeal.setSubjectType("6");
			moneyDeal.setRemark("会员激活");
			moneyDealDao.save(moneyDeal);
			user.setActivity("0");
			user.setActivityDate(time); //激活时间
			user.setRegisterId(recommendUser.getUserAccount()); //激活人
			userInfoDao.update(user);
			
			List<User> fatherList = getUser(user.getParentId());
			User fatherUser = null;
			if(fatherList != null && fatherList.size() > 0){
				fatherUser = fatherList.get(0);
			}
			//推荐人数加1          
			//recommendUser.setRecommendCnt(new Integer(recommendUser.getRecommendCnt() + 1).toString());
			//推荐人的直推奖
			DicItem dicItem2 = new DicItem();
			dicItem2.setType(ShopConstants.LEVEL);
			dicItem2.setCode(recommendUser.getLevel());
			List<DicItem> recommendLevelList = dicItemDao.findByExample(dicItem2); 
			DicItem recommendLevel = null;
			if(recommendLevelList != null && recommendLevelList.size() > 0){
				recommendLevel = recommendLevelList.get(0);
			}
			BigDecimal directPrize = new BigDecimal(recommendLevel.getAttr3()).multiply(new BigDecimal(dicItem.getAttr1()));
			//推荐人的直推奖计入账号和流水账
			DicItem prize1 = dicItemService.findByTypeAndCode(ShopConstants.PRIZE_SCALE, "1");
			DicItem prize2 = dicItemService.findByTypeAndCode(ShopConstants.PRIZE_SCALE, "2");
			DicItem prize3 = dicItemService.findByTypeAndCode(ShopConstants.PRIZE_SCALE, "3");
			BigDecimal prizeRate = new BigDecimal(prize1.getAttr1());  //0.88
			BigDecimal consumeRate = new BigDecimal(prize2.getAttr1()); //0.1
			BigDecimal loveRate = new BigDecimal(prize3.getAttr1());  //0.02
			
			BigDecimal directPrizet = directPrize.multiply(prizeRate);
			BigDecimal directPrizea = directPrize.multiply(consumeRate);
			BigDecimal directPrizel =  directPrize.multiply(loveRate);
			recommendAccount.setBonusMoney(recommendAccount.getBonusMoney().add(directPrizet));
			recommendAccount.setConsumerMoney(recommendAccount.getConsumerMoney().add(directPrizea));
			recommendAccount.setRecommendMoney(recommendAccount.getRecommendMoney().add(directPrizel));
			userAccountDao.save(recommendAccount);
			MoneyDeal recommendmoneyDeal = new MoneyDeal();
			recommendmoneyDeal.setAccountSubject(ShopConstants.DIRECT);
			recommendmoneyDeal.setAccountType(ShopConstants.PRIZE);
			recommendmoneyDeal.setCreateDate(time);
			recommendmoneyDeal.setIncome(directPrizet);
			recommendmoneyDeal.setUserId(recommendUser.getUserId());
			recommendmoneyDeal.setSubjectType("1");
			recommendmoneyDeal.setRemark(user.getUserAccount());
			recommendmoneyDeal.setDealId(user.getUserAccount());
			moneyDealDao.save(recommendmoneyDeal);
			//
			PrizePay pay = new PrizePay();
			pay.setCreateDate(time);
			pay.setPushPrize(directPrizet);
			pay.setConsumerPrize(directPrizea);
			pay.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
			pay.setRelationUser(user.getUserAccount());
			pay.setUserId(recommendUser.getUserAccount());
			pay.setLoveFund(directPrizel);
			prizePayDao.save(pay);
			//重消
			MoneyDeal recommendmoneyDeal2 = new MoneyDeal();
			recommendmoneyDeal2.setAccountSubject(ShopConstants.RECONSUME);
			recommendmoneyDeal2.setAccountType(ShopConstants.RESUMER);
			recommendmoneyDeal2.setCreateDate(time);
			recommendmoneyDeal2.setIncome(directPrizea);
			recommendmoneyDeal2.setUserId(recommendUser.getUserId());
			recommendmoneyDeal2.setSubjectType("1");
			recommendmoneyDeal2.setDealId(user.getUserAccount());
			recommendmoneyDeal2.setRemark(user.getUserAccount());
			moneyDealDao.save(recommendmoneyDeal2);
			//爱心基金
			MoneyDeal recommendmoneyDeal3 = new MoneyDeal();
			recommendmoneyDeal3.setAccountSubject(ShopConstants.LOVE);
			recommendmoneyDeal3.setAccountType(ShopConstants.LOVE_FUND);
			recommendmoneyDeal3.setCreateDate(time);
			recommendmoneyDeal3.setIncome(directPrizel);
			recommendmoneyDeal3.setUserId(recommendUser.getUserId());
			recommendmoneyDeal3.setSubjectType("1");
			recommendmoneyDeal3.setDealId(user.getUserAccount());
			recommendmoneyDeal3.setRemark(user.getUserAccount());
			moneyDealDao.save(recommendmoneyDeal2);
			
			//对碰奖金率
			DicItem bounsItem = new DicItem();
			bounsItem.setType(ShopConstants.BONUS);
			List<DicItem> bonusList = dicItemDao.findByExample(bounsItem);
			BigDecimal bonusRate = null;
			if(bonusList != null && bonusList.size() > 0){
				bonusRate = new BigDecimal(bonusList.get(0).getCode());
			}
			
			List<User> parentList = new ArrayList<User>();
			findParent(parentList, user);//链接父节点
			String location = user.getLocation();
			Integer leftleave = null;
			BigDecimal leftleaveAmount = null;
			Integer rightleave = null;
			BigDecimal rightleaveAmount = null;
			BigDecimal levelamount = new BigDecimal(dicItem.getAttr1());
			BigDecimal bonus = null; //对碰奖金
			MoneyDeal deal = null;
			MoneyDeal deal2 = null;
			MoneyDeal deal3 = null;
			UserAccount parentAccount = null;
			Integer topBonus = 0;
			BigDecimal totalBounsDay = BigDecimal.ZERO;
			int floor = 0;
			int billCnt = Integer.parseInt(dicItem.getAttr1())/600;
			User leader1 = null;
			User leader2 = null;
			User leader3 = null;
			UserAccount leaderAccount1 = null;
			UserAccount leaderAccount2 = null;
			UserAccount leaderAccount3 = null;
			BigDecimal leadprize = BigDecimal.ZERO;
			for (User parent : parentList) {
				if("1".equals(location)){  //1:左区          2:右区
					parent.setSparel(parent.getSparel() + billCnt);
					parent.setSparelAmount(parent.getSparelAmount().add(levelamount));
					leftleave = parent.getLeftLeave() + billCnt;  //现有的
					leftleaveAmount = parent.getLeftLeaveAmount().add(levelamount); //现有的     >0
					rightleave = parent.getRightLeave();
					rightleaveAmount = parent.getRightLeaveAmount();
					if(leftleaveAmount.compareTo(rightleaveAmount)== 0){ //相等						
						//全部对碰
						bonus = leftleaveAmount.multiply(bonusRate);  //对碰奖金
						parent.setLeftLeave(0);
						parent.setLeftLeaveAmount(BigDecimal.ZERO);
						parent.setRightLeave(0);
						parent.setRightLeaveAmount(BigDecimal.ZERO);
						userInfoDao.update(parent);
					}else if(leftleaveAmount.compareTo(rightleaveAmount)== -1){ //小于   左边小于右边   把左边对碰掉
						bonus = leftleaveAmount.multiply(bonusRate);//对碰奖金
						parent.setLeftLeave(0);
						parent.setLeftLeaveAmount(BigDecimal.ZERO);
						parent.setRightLeave(parent.getRightLeave()-billCnt);
						parent.setRightLeaveAmount(rightleaveAmount.subtract(leftleaveAmount)); //右边减去左边 剩余的
						userInfoDao.update(parent);
						
					}else if(leftleaveAmount.compareTo(rightleaveAmount)== 1){ //大于  左边大于右边
						if(rightleaveAmount.compareTo(BigDecimal.ZERO) == 0){//相等0
							parent.setLeftLeave(leftleave);
							parent.setLeftLeaveAmount(leftleaveAmount);
							userInfoDao.update(parent);
							//没有对碰奖
							bonus = null;
						}else if(rightleaveAmount.compareTo(BigDecimal.ZERO) == 1){//大于0
							bonus = rightleaveAmount.multiply(bonusRate); //对碰奖金
							parent.setLeftLeave(leftleave-billCnt);
							parent.setLeftLeaveAmount(leftleaveAmount.subtract(rightleaveAmount));
							parent.setRightLeave(0);
							parent.setRightLeaveAmount(BigDecimal.ZERO);
							userInfoDao.update(parent);
						}	
					}
				}else{ // 2 右区
					parent.setSparer(parent.getSparer()+1);
					parent.setApaerAmount(parent.getApaerAmount().add(levelamount));
					leftleave = parent.getLeftLeave();  //现有的
					leftleaveAmount = parent.getLeftLeaveAmount(); //现有的
					rightleave = parent.getRightLeave()+billCnt;
					rightleaveAmount = parent.getRightLeaveAmount().add(levelamount);  //右边大于0
					if(leftleaveAmount.compareTo(rightleaveAmount)== 0){ //相等
						//全部对碰
						bonus = leftleaveAmount.multiply(bonusRate);  //对碰奖金
						parent.setLeftLeave(0);
						parent.setLeftLeaveAmount(BigDecimal.ZERO);
						parent.setRightLeave(0);
						parent.setRightLeaveAmount(BigDecimal.ZERO);
						userInfoDao.update(parent);
					}else if(leftleaveAmount.compareTo(rightleaveAmount)== 1){//左边大于右边
						//对碰掉右边
						bonus = rightleaveAmount.multiply(bonusRate);  //对碰奖金
						parent.setLeftLeave(leftleave-billCnt);
						parent.setLeftLeaveAmount(leftleaveAmount.subtract(rightleaveAmount));
						parent.setRightLeave(0);
						parent.setRightLeaveAmount(BigDecimal.ZERO);
						userInfoDao.update(parent);
						
					}else if(leftleaveAmount.compareTo(rightleaveAmount)== -1){//右边大于左边
						if(leftleaveAmount.compareTo(BigDecimal.ZERO) == 0){
							parent.setRightLeave(rightleave);
							parent.setRightLeaveAmount(rightleaveAmount);
							userInfoDao.update(parent);
							//没有对碰奖
							bonus = null;
						}else if(leftleaveAmount.compareTo(BigDecimal.ZERO) == 1){//左边大于0  对碰掉左边的
							bonus = leftleaveAmount.multiply(bonusRate);
							parent.setLeftLeave(0);
							parent.setLeftLeaveAmount(BigDecimal.ZERO);
							parent.setRightLeave(rightleave-billCnt);
							parent.setRightLeaveAmount(rightleaveAmount.subtract(leftleaveAmount));
							userInfoDao.update(parent);
							//parent.
						}
					}
				}
				if(bonus != null){
					
					//重消奖  奖金的10%   那么实际的奖金是90%
					
					//计算奖金封顶值   查询出当前一天的对碰奖总数
					//根据会员级别，得到封顶值，取出当天总共的对碰奖金  与封顶值比较若超出则扣掉超出的那部分
					//封顶值
					topBonus = dicItemService.findByTypeAndCode(ShopConstants.LEVEL, parent.getLevel()).getSeg1();
					//今天的总对碰奖
					totalBounsDay = moneyDealDao.findBonusByDay(parent.getUserId(), date);
					if(totalBounsDay.compareTo(new BigDecimal(topBonus)) == 0){//相等  已经封顶了 不在加奖
						
					}else{  //未到封顶值时
						totalBounsDay = totalBounsDay.add(bonus.multiply(prizeRate));  //加上本次的
						if(totalBounsDay.compareTo(new BigDecimal(topBonus)) == 0 || totalBounsDay.compareTo(new BigDecimal(topBonus)) == -1){ //恰好到封顶值
							deal = new MoneyDeal();
							deal.setAccountSubject(ShopConstants.BONUSS);
							deal.setAccountType(ShopConstants.PRIZE);
							deal.setCreateDate(time);
							deal.setIncome(bonus.multiply(prizeRate));
							deal.setSubjectType("1");
							deal.setUserId(parent.getUserId());
							deal.setRemark(user.getUserAccount());
							deal.setDealId(user.getUserAccount());
							moneyDealDao.save(deal);	
							deal2 = new MoneyDeal();
							deal2.setAccountSubject(ShopConstants.RECONSUME);
							deal2.setAccountType(ShopConstants.RESUMER);
							deal2.setCreateDate(time);
							deal2.setIncome(bonus.multiply(consumeRate));
							deal2.setSubjectType("1");
							deal2.setUserId(parent.getUserId());
							deal2.setRemark(user.getUserAccount());
							//deal.setDealId(user.getUserAccount());
							moneyDealDao.save(deal2);
							
							//爱心基金
							deal3 = new MoneyDeal();
							deal3.setAccountSubject(ShopConstants.LOVE);
							deal3.setAccountType(ShopConstants.LOVE_FUND);
							deal3.setCreateDate(time);
							deal3.setIncome(bonus.multiply(loveRate));
							deal3.setUserId(parent.getUserId());
							deal3.setSubjectType("1");
							//deal3.setDealId(user.getUserAccount());
							deal3.setRemark(user.getUserAccount());
							moneyDealDao.save(deal3);
							
							PrizePay paybouns = new PrizePay();
							paybouns.setCreateDate(time);
							paybouns.setPairsPrize(bonus.multiply(prizeRate));
							paybouns.setConsumerPrize(bonus.multiply(consumeRate));
							paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
							paybouns.setRelationUser(user.getUserAccount());
							paybouns.setUserId(parent.getUserAccount());
							paybouns.setLoveFund(bonus.multiply(loveRate));
							prizePayDao.save(paybouns);
							
							//奖金累加到个人账户
							parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
							parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(bonus.multiply(prizeRate)));  //88%的奖金
							parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(bonus.multiply(consumeRate))); //10%的重消
							parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(bonus.multiply(loveRate))); //2%的爱心基金
							userAccountDao.update(parentAccount);	
							
							//领导奖    拿到对碰奖的向上找三代推荐人计算
							//第一代推荐人， 直接推荐人
							leader1 = userInfoService.findByAccount(parent.getRecommendId());
							if(leader1 != null){
								leadprize = bonus.multiply(new BigDecimal(0.1));
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.LEADER);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(leadprize.multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(leader1.getUserId());
								deal.setRemark(parent.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(leadprize.multiply(consumeRate));
								deal2.setSubjectType("1");
								deal2.setUserId(leader1.getUserId());
								deal2.setRemark(parent.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(leadprize.multiply(loveRate));
								deal3.setUserId(leader1.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(parent.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybounsl = new PrizePay();
								paybounsl.setCreateDate(time);
								paybounsl.setLeaderPrize(leadprize.multiply(prizeRate));
								paybounsl.setConsumerPrize(leadprize.multiply(consumeRate));
								paybounsl.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
								paybounsl.setRelationUser(user.getUserAccount());
								paybounsl.setUserId(leader1.getUserAccount());
								paybounsl.setLoveFund(leadprize.multiply(loveRate));
								prizePayDao.save(paybounsl);
								leaderAccount1 = userAccountDao.findByAccount(leader1.getUserAccount());
								leaderAccount1.setBonusMoney(leaderAccount1.getBonusMoney().add(leadprize.multiply(prizeRate)));
								leaderAccount1.setConsumerMoney(leaderAccount1.getConsumerMoney().add(leadprize.multiply(consumeRate)));
								leaderAccount1.setRecommendMoney(leaderAccount1.getRecommendMoney().add(leadprize.multiply(loveRate)));
								userAccountDao.update(leaderAccount1);
								//第二代
								leader2 = userInfoService.findByAccount(leader1.getRecommendId());
								if(leader2 != null){
									if("2".equals(leader2.getLevel())){  //二星  二代的5%
										leadprize = bonus.multiply(new BigDecimal(0.05));
										deal = new MoneyDeal();
										deal.setAccountSubject(ShopConstants.LEADER);
										deal.setAccountType(ShopConstants.PRIZE);
										deal.setCreateDate(time);
										deal.setIncome(leadprize.multiply(prizeRate));
										deal.setSubjectType("1");
										deal.setUserId(leader2.getUserId());
										deal.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal);	
										deal2 = new MoneyDeal();
										deal2.setAccountSubject(ShopConstants.RECONSUME);
										deal2.setAccountType(ShopConstants.RESUMER);
										deal2.setCreateDate(time);
										deal2.setIncome(leadprize.multiply(consumeRate));
										deal2.setSubjectType("1");
										deal2.setUserId(leader2.getUserId());
										deal2.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal2);
										
										//爱心基金
										deal3 = new MoneyDeal();
										deal3.setAccountSubject(ShopConstants.LOVE);
										deal3.setAccountType(ShopConstants.LOVE_FUND);
										deal3.setCreateDate(time);
										deal3.setIncome(leadprize.multiply(loveRate));
										deal3.setUserId(leader2.getUserId());
										deal3.setSubjectType("1");
										//deal3.setDealId(user.getUserAccount());
										deal3.setRemark(parent.getUserAccount());
										moneyDealDao.save(deal3);
										
										PrizePay paybouns2 = new PrizePay();
										paybouns2.setCreateDate(time);
										paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
										paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
										paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
										paybouns2.setRelationUser(user.getUserAccount());
										paybouns2.setUserId(leader2.getUserAccount());
										paybouns2.setLoveFund(leadprize.multiply(loveRate));
										prizePayDao.save(paybouns2);
										leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
										leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
										leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
										leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
										userAccountDao.update(leaderAccount2);
									}else if("3".equals(leader2.getLevel()) || "4".equals(leader2.getLevel()) || "5".equals(leader2.getLevel())){ //三星 四星 五星 10%
										leadprize = bonus.multiply(new BigDecimal(0.1));
										deal = new MoneyDeal();
										deal.setAccountSubject(ShopConstants.LEADER);
										deal.setAccountType(ShopConstants.PRIZE);
										deal.setCreateDate(time);
										deal.setIncome(leadprize.multiply(prizeRate));
										deal.setSubjectType("1");
										deal.setUserId(leader2.getUserId());
										deal.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal);	
										deal2 = new MoneyDeal();
										deal2.setAccountSubject(ShopConstants.RECONSUME);
										deal2.setAccountType(ShopConstants.RESUMER);
										deal2.setCreateDate(time);
										deal2.setIncome(leadprize.multiply(consumeRate));
										deal2.setSubjectType("1");
										deal2.setUserId(leader2.getUserId());
										deal2.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal2);
										
										//爱心基金
										deal3 = new MoneyDeal();
										deal3.setAccountSubject(ShopConstants.LOVE);
										deal3.setAccountType(ShopConstants.LOVE_FUND);
										deal3.setCreateDate(time);
										deal3.setIncome(leadprize.multiply(loveRate));
										deal3.setUserId(leader2.getUserId());
										deal3.setSubjectType("1");
										//deal3.setDealId(user.getUserAccount());
										deal3.setRemark(parent.getUserAccount());
										moneyDealDao.save(deal3);
										
										PrizePay paybouns2 = new PrizePay();
										paybouns2.setCreateDate(time);
										paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
										paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
										paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
										paybouns2.setRelationUser(user.getUserAccount());
										paybouns2.setUserId(leader2.getUserAccount());
										paybouns2.setLoveFund(leadprize.multiply(loveRate));
										prizePayDao.save(paybouns2);
										leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
										leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
										leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
										leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
										userAccountDao.update(leaderAccount2);
									}
									//第三代
									leader3 = userInfoService.findByAccount(leader2.getRecommendId());
									if(leader3 != null){
										if("4".equals(leader3.getLevel())){  //四星  三代的5%
											leadprize = bonus.multiply(new BigDecimal(0.05));
											deal = new MoneyDeal();
											deal.setAccountSubject(ShopConstants.LEADER);
											deal.setAccountType(ShopConstants.PRIZE);
											deal.setCreateDate(time);
											deal.setIncome(leadprize.multiply(prizeRate));
											deal.setSubjectType("1");
											deal.setUserId(leader3.getUserId());
											deal.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal);	
											deal2 = new MoneyDeal();
											deal2.setAccountSubject(ShopConstants.RECONSUME);
											deal2.setAccountType(ShopConstants.RESUMER);
											deal2.setCreateDate(time);
											deal2.setIncome(leadprize.multiply(consumeRate));
											deal2.setSubjectType("1");
											deal2.setUserId(leader3.getUserId());
											deal2.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal2);
											
											//爱心基金
											deal3 = new MoneyDeal();
											deal3.setAccountSubject(ShopConstants.LOVE);
											deal3.setAccountType(ShopConstants.LOVE_FUND);
											deal3.setCreateDate(time);
											deal3.setIncome(leadprize.multiply(loveRate));
											deal3.setUserId(leader3.getUserId());
											deal3.setSubjectType("1");
											//deal3.setDealId(user.getUserAccount());
											deal3.setRemark(parent.getUserAccount());
											moneyDealDao.save(deal3);
											
											PrizePay paybouns2 = new PrizePay();
											paybouns2.setCreateDate(time);
											paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
											paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
											paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
											paybouns2.setRelationUser(user.getUserAccount());
											paybouns2.setUserId(leader3.getUserAccount());
											paybouns2.setLoveFund(leadprize.multiply(loveRate));
											prizePayDao.save(paybouns2);
											leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
											leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
											leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
											leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
											userAccountDao.update(leaderAccount3);
										}else if("5".equals(leader3.getLevel())){ //五星 10%
											leadprize = bonus.multiply(new BigDecimal(0.1));
											deal = new MoneyDeal();
											deal.setAccountSubject(ShopConstants.LEADER);
											deal.setAccountType(ShopConstants.PRIZE);
											deal.setCreateDate(time);
											deal.setIncome(leadprize.multiply(prizeRate));
											deal.setSubjectType("1");
											deal.setUserId(leader3.getUserId());
											deal.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal);	
											deal2 = new MoneyDeal();
											deal2.setAccountSubject(ShopConstants.RECONSUME);
											deal2.setAccountType(ShopConstants.RESUMER);
											deal2.setCreateDate(time);
											deal2.setIncome(leadprize.multiply(consumeRate));
											deal2.setSubjectType("1");
											deal2.setUserId(leader3.getUserId());
											deal2.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal2);
											
											//爱心基金
											deal3 = new MoneyDeal();
											deal3.setAccountSubject(ShopConstants.LOVE);
											deal3.setAccountType(ShopConstants.LOVE_FUND);
											deal3.setCreateDate(time);
											deal3.setIncome(leadprize.multiply(loveRate));
											deal3.setUserId(leader3.getUserId());
											deal3.setSubjectType("1");
											//deal3.setDealId(user.getUserAccount());
											deal3.setRemark(parent.getUserAccount());
											moneyDealDao.save(deal3);
											
											PrizePay paybouns2 = new PrizePay();
											paybouns2.setCreateDate(time);
											paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
											paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
											paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
											paybouns2.setRelationUser(user.getUserAccount());
											paybouns2.setUserId(leader3.getUserAccount());
											paybouns2.setLoveFund(leadprize.multiply(loveRate));
											prizePayDao.save(paybouns2);
											leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
											leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
											leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
											leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
											userAccountDao.update(leaderAccount3);
										}
									}
								}
							}
							
							
							
						}else if(totalBounsDay.compareTo(new BigDecimal(topBonus)) == 1){  //超过封顶
							deal = new MoneyDeal();
							deal.setAccountSubject(ShopConstants.BONUSS);
							deal.setAccountType(ShopConstants.PRIZE);
							deal.setCreateDate(time);
							deal.setIncome(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(prizeRate));
							deal.setSubjectType("1");
							deal.setUserId(parent.getUserId());
							deal.setDealId(user.getUserAccount());
							deal.setRemark(user.getUserAccount());
							moneyDealDao.save(deal);	
							deal2 = new MoneyDeal();
							deal2.setAccountSubject(ShopConstants.RECONSUME);
							deal2.setAccountType(ShopConstants.RESUMER);
							deal2.setCreateDate(time);
							deal2.setIncome(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(consumeRate));
							deal2.setSubjectType("1");
							deal2.setUserId(parent.getUserId());
							deal2.setRemark(user.getUserAccount());
							//deal.setDealId(user.getUserAccount());
							moneyDealDao.save(deal2);
							
							//爱心基金
							deal3 = new MoneyDeal();
							deal3.setAccountSubject(ShopConstants.LOVE);
							deal3.setAccountType(ShopConstants.LOVE_FUND);
							deal3.setCreateDate(time);
							deal3.setIncome(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(loveRate));
							deal3.setUserId(recommendUser.getUserId());
							deal3.setSubjectType("1");
							//deal3.setDealId(user.getUserAccount());
							deal3.setRemark(user.getUserAccount());
							moneyDealDao.save(deal3);
							
							PrizePay paybouns = new PrizePay();
							paybouns.setCreateDate(time);
							paybouns.setPairsPrize(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(prizeRate));
							paybouns.setConsumerPrize(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(consumeRate));
							paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
							paybouns.setRelationUser(user.getUserAccount());
							paybouns.setUserId(parent.getUserAccount());
							paybouns.setLoveFund(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(loveRate));
							prizePayDao.save(paybouns);
							
							//奖金累加到个人账户
							parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
							parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(prizeRate)));
							parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(consumeRate)));
							parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(loveRate)));
							userAccountDao.update(parentAccount);
							
							//领导奖    拿到对碰奖的向上找三代推荐人计算
							//第一代推荐人， 直接推荐人
							leader1 = userInfoService.findByAccount(parent.getRecommendId());
							if(leader1 != null){
								leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.1));
								deal = new MoneyDeal();
								deal.setAccountSubject(ShopConstants.LEADER);
								deal.setAccountType(ShopConstants.PRIZE);
								deal.setCreateDate(time);
								deal.setIncome(leadprize.multiply(prizeRate));
								deal.setSubjectType("1");
								deal.setUserId(leader1.getUserId());
								deal.setRemark(parent.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal);	
								deal2 = new MoneyDeal();
								deal2.setAccountSubject(ShopConstants.RECONSUME);
								deal2.setAccountType(ShopConstants.RESUMER);
								deal2.setCreateDate(time);
								deal2.setIncome(leadprize.multiply(consumeRate));
								deal2.setSubjectType("1");
								deal2.setUserId(leader1.getUserId());
								deal2.setRemark(parent.getUserAccount());
								//deal.setDealId(user.getUserAccount());
								moneyDealDao.save(deal2);
								
								//爱心基金
								deal3 = new MoneyDeal();
								deal3.setAccountSubject(ShopConstants.LOVE);
								deal3.setAccountType(ShopConstants.LOVE_FUND);
								deal3.setCreateDate(time);
								deal3.setIncome(leadprize.multiply(loveRate));
								deal3.setUserId(leader1.getUserId());
								deal3.setSubjectType("1");
								//deal3.setDealId(user.getUserAccount());
								deal3.setRemark(parent.getUserAccount());
								moneyDealDao.save(deal3);
								
								PrizePay paybounsl = new PrizePay();
								paybounsl.setCreateDate(time);
								paybounsl.setLeaderPrize(leadprize.multiply(prizeRate));
								paybounsl.setConsumerPrize(leadprize.multiply(consumeRate));
								paybounsl.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
								paybounsl.setRelationUser(user.getUserAccount());
								paybounsl.setUserId(leader1.getUserAccount());
								paybounsl.setLoveFund(leadprize.multiply(loveRate));
								prizePayDao.save(paybounsl);
								leaderAccount1 = userAccountDao.findByAccount(leader1.getUserAccount());
								leaderAccount1.setBonusMoney(leaderAccount1.getBonusMoney().add(leadprize.multiply(prizeRate)));
								leaderAccount1.setConsumerMoney(leaderAccount1.getConsumerMoney().add(leadprize.multiply(consumeRate)));
								leaderAccount1.setRecommendMoney(leaderAccount1.getRecommendMoney().add(leadprize.multiply(loveRate)));
								userAccountDao.update(leaderAccount1);
								//第二代
								leader2 = userInfoService.findByAccount(leader1.getRecommendId());
								if(leader2 != null){
									if("2".equals(leader2.getLevel())){  //二星  二代的5%
										leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.05));
										deal = new MoneyDeal();
										deal.setAccountSubject(ShopConstants.LEADER);
										deal.setAccountType(ShopConstants.PRIZE);
										deal.setCreateDate(time);
										deal.setIncome(leadprize.multiply(prizeRate));
										deal.setSubjectType("1");
										deal.setUserId(leader2.getUserId());
										deal.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal);	
										deal2 = new MoneyDeal();
										deal2.setAccountSubject(ShopConstants.RECONSUME);
										deal2.setAccountType(ShopConstants.RESUMER);
										deal2.setCreateDate(time);
										deal2.setIncome(leadprize.multiply(consumeRate));
										deal2.setSubjectType("1");
										deal2.setUserId(leader2.getUserId());
										deal2.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal2);
										
										//爱心基金
										deal3 = new MoneyDeal();
										deal3.setAccountSubject(ShopConstants.LOVE);
										deal3.setAccountType(ShopConstants.LOVE_FUND);
										deal3.setCreateDate(time);
										deal3.setIncome(leadprize.multiply(loveRate));
										deal3.setUserId(leader2.getUserId());
										deal3.setSubjectType("1");
										//deal3.setDealId(user.getUserAccount());
										deal3.setRemark(parent.getUserAccount());
										moneyDealDao.save(deal3);
										
										PrizePay paybouns2 = new PrizePay();
										paybouns2.setCreateDate(time);
										paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
										paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
										paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
										paybouns2.setRelationUser(user.getUserAccount());
										paybouns2.setUserId(leader2.getUserAccount());
										paybouns2.setLoveFund(leadprize.multiply(loveRate));
										prizePayDao.save(paybouns2);
										leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
										leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
										leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
										leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
										userAccountDao.update(leaderAccount2);
									}else if("3".equals(leader2.getLevel()) || "4".equals(leader2.getLevel()) || "5".equals(leader2.getLevel())){ //三星 四星 五星 10%
										leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.1));
										deal = new MoneyDeal();
										deal.setAccountSubject(ShopConstants.LEADER);
										deal.setAccountType(ShopConstants.PRIZE);
										deal.setCreateDate(time);
										deal.setIncome(leadprize.multiply(prizeRate));
										deal.setSubjectType("1");
										deal.setUserId(leader2.getUserId());
										deal.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal);	
										deal2 = new MoneyDeal();
										deal2.setAccountSubject(ShopConstants.RECONSUME);
										deal2.setAccountType(ShopConstants.RESUMER);
										deal2.setCreateDate(time);
										deal2.setIncome(leadprize.multiply(consumeRate));
										deal2.setSubjectType("1");
										deal2.setUserId(leader2.getUserId());
										deal2.setRemark(parent.getUserAccount());
										//deal.setDealId(user.getUserAccount());
										moneyDealDao.save(deal2);
										
										//爱心基金
										deal3 = new MoneyDeal();
										deal3.setAccountSubject(ShopConstants.LOVE);
										deal3.setAccountType(ShopConstants.LOVE_FUND);
										deal3.setCreateDate(time);
										deal3.setIncome(leadprize.multiply(loveRate));
										deal3.setUserId(leader2.getUserId());
										deal3.setSubjectType("1");
										//deal3.setDealId(user.getUserAccount());
										deal3.setRemark(parent.getUserAccount());
										moneyDealDao.save(deal3);
										
										PrizePay paybouns2 = new PrizePay();
										paybouns2.setCreateDate(time);
										paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
										paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
										paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
										paybouns2.setRelationUser(user.getUserAccount());
										paybouns2.setUserId(leader2.getUserAccount());
										paybouns2.setLoveFund(leadprize.multiply(loveRate));
										prizePayDao.save(paybouns2);
										leaderAccount2 = userAccountDao.findByAccount(leader2.getUserAccount());
										leaderAccount2.setBonusMoney(leaderAccount2.getBonusMoney().add(leadprize.multiply(prizeRate)));
										leaderAccount2.setConsumerMoney(leaderAccount2.getConsumerMoney().add(leadprize.multiply(consumeRate)));
										leaderAccount2.setRecommendMoney(leaderAccount2.getRecommendMoney().add(leadprize.multiply(loveRate)));
										userAccountDao.update(leaderAccount2);
									}
									//第三代
									leader3 = userInfoService.findByAccount(leader2.getRecommendId());
									if(leader3 != null){
										if("4".equals(leader3.getLevel())){  //四星  三代的5%
											leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.05));
											deal = new MoneyDeal();
											deal.setAccountSubject(ShopConstants.LEADER);
											deal.setAccountType(ShopConstants.PRIZE);
											deal.setCreateDate(time);
											deal.setIncome(leadprize.multiply(prizeRate));
											deal.setSubjectType("1");
											deal.setUserId(leader3.getUserId());
											deal.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal);	
											deal2 = new MoneyDeal();
											deal2.setAccountSubject(ShopConstants.RECONSUME);
											deal2.setAccountType(ShopConstants.RESUMER);
											deal2.setCreateDate(time);
											deal2.setIncome(leadprize.multiply(consumeRate));
											deal2.setSubjectType("1");
											deal2.setUserId(leader3.getUserId());
											deal2.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal2);
											
											//爱心基金
											deal3 = new MoneyDeal();
											deal3.setAccountSubject(ShopConstants.LOVE);
											deal3.setAccountType(ShopConstants.LOVE_FUND);
											deal3.setCreateDate(time);
											deal3.setIncome(leadprize.multiply(loveRate));
											deal3.setUserId(leader3.getUserId());
											deal3.setSubjectType("1");
											//deal3.setDealId(user.getUserAccount());
											deal3.setRemark(parent.getUserAccount());
											moneyDealDao.save(deal3);
											
											PrizePay paybouns2 = new PrizePay();
											paybouns2.setCreateDate(time);
											paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
											paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
											paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
											paybouns2.setRelationUser(user.getUserAccount());
											paybouns2.setUserId(leader3.getUserAccount());
											paybouns2.setLoveFund(leadprize.multiply(loveRate));
											prizePayDao.save(paybouns2);
											leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
											leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
											leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
											leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
											userAccountDao.update(leaderAccount3);
										}else if("5".equals(leader3.getLevel())){ //五星 10%
											leadprize = bonus.subtract(totalBounsDay.subtract(new BigDecimal(topBonus))).multiply(new BigDecimal(0.1));
											deal = new MoneyDeal();
											deal.setAccountSubject(ShopConstants.LEADER);
											deal.setAccountType(ShopConstants.PRIZE);
											deal.setCreateDate(time);
											deal.setIncome(leadprize.multiply(prizeRate));
											deal.setSubjectType("1");
											deal.setUserId(leader3.getUserId());
											deal.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal);	
											deal2 = new MoneyDeal();
											deal2.setAccountSubject(ShopConstants.RECONSUME);
											deal2.setAccountType(ShopConstants.RESUMER);
											deal2.setCreateDate(time);
											deal2.setIncome(leadprize.multiply(consumeRate));
											deal2.setSubjectType("1");
											deal2.setUserId(leader3.getUserId());
											deal2.setRemark(parent.getUserAccount());
											//deal.setDealId(user.getUserAccount());
											moneyDealDao.save(deal2);
											
											//爱心基金
											deal3 = new MoneyDeal();
											deal3.setAccountSubject(ShopConstants.LOVE);
											deal3.setAccountType(ShopConstants.LOVE_FUND);
											deal3.setCreateDate(time);
											deal3.setIncome(leadprize.multiply(loveRate));
											deal3.setUserId(leader3.getUserId());
											deal3.setSubjectType("1");
											//deal3.setDealId(user.getUserAccount());
											deal3.setRemark(parent.getUserAccount());
											moneyDealDao.save(deal3);
											
											PrizePay paybouns2 = new PrizePay();
											paybouns2.setCreateDate(time);
											paybouns2.setLeaderPrize(leadprize.multiply(prizeRate));
											paybouns2.setConsumerPrize(leadprize.multiply(consumeRate));
											paybouns2.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
											paybouns2.setRelationUser(user.getUserAccount());
											paybouns2.setUserId(leader3.getUserAccount());
											paybouns2.setLoveFund(leadprize.multiply(loveRate));
											prizePayDao.save(paybouns2);
											leaderAccount3 = userAccountDao.findByAccount(leader2.getUserAccount());
											leaderAccount3.setBonusMoney(leaderAccount3.getBonusMoney().add(leadprize.multiply(prizeRate)));
											leaderAccount3.setConsumerMoney(leaderAccount3.getConsumerMoney().add(leadprize.multiply(consumeRate)));
											leaderAccount3.setRecommendMoney(leaderAccount3.getRecommendMoney().add(leadprize.multiply(loveRate)));
											userAccountDao.update(leaderAccount3);
										}
									}
								}
							}
						}
					
					}				
				}
				location = parent.getLocation();
				
				floor += 1;
				//见点奖
				if("1".equals(parent.getLevel())){  //一星的   见点奖  2层  4层
					if(floor == 2 || floor ==4){
						deal = new MoneyDeal();
						deal.setAccountSubject(ShopConstants.POINT);
						deal.setAccountType(ShopConstants.PRIZE);
						deal.setCreateDate(time);
						deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						deal.setSubjectType("1");
						deal.setUserId(parent.getUserId());
						deal.setDealId(user.getUserAccount());
						deal.setRemark(user.getUserAccount());
						moneyDealDao.save(deal);	
						deal2 = new MoneyDeal();
						deal2.setAccountSubject(ShopConstants.RECONSUME);
						deal2.setAccountType(ShopConstants.RESUMER);
						deal2.setCreateDate(time);
						deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
						deal2.setSubjectType("1");
						deal2.setUserId(parent.getUserId());
						deal2.setRemark(user.getUserAccount());
						//deal.setDealId(user.getUserAccount());
						moneyDealDao.save(deal2);
						
						//爱心基金
						deal3 = new MoneyDeal();
						deal3.setAccountSubject(ShopConstants.LOVE);
						deal3.setAccountType(ShopConstants.LOVE_FUND);
						deal3.setCreateDate(time);
						deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
						deal3.setUserId(parent.getUserId());
						deal3.setSubjectType("1");
						//deal3.setDealId(user.getUserAccount());
						deal3.setRemark(user.getUserAccount());
						moneyDealDao.save(deal3);
						
						PrizePay paybouns = new PrizePay();
						paybouns.setCreateDate(time);
						paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
						paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
						paybouns.setRelationUser(user.getUserAccount());
						paybouns.setUserId(parent.getUserAccount());
						paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
						prizePayDao.save(paybouns);
						
						//奖金累加到个人账户
						parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
						parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
						parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate)));
						parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate)));
						userAccountDao.update(parentAccount);
					}
				}else if("2".equals(parent.getLevel())){
					if(floor == 2 || floor ==4 || floor ==6){
						deal = new MoneyDeal();
						deal.setAccountSubject(ShopConstants.POINT);
						deal.setAccountType(ShopConstants.PRIZE);
						deal.setCreateDate(time);
						deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						deal.setSubjectType("1");
						deal.setUserId(parent.getUserId());
						deal.setDealId(user.getUserAccount());
						deal.setRemark(user.getUserAccount());
						moneyDealDao.save(deal);	
						deal2 = new MoneyDeal();
						deal2.setAccountSubject(ShopConstants.RECONSUME);
						deal2.setAccountType(ShopConstants.RESUMER);
						deal2.setCreateDate(time);
						deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
						deal2.setSubjectType("1");
						deal2.setUserId(parent.getUserId());
						deal2.setRemark(user.getUserAccount());
						//deal.setDealId(user.getUserAccount());
						moneyDealDao.save(deal2);
						
						//爱心基金
						deal3 = new MoneyDeal();
						deal3.setAccountSubject(ShopConstants.LOVE);
						deal3.setAccountType(ShopConstants.LOVE_FUND);
						deal3.setCreateDate(time);
						deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
						deal3.setUserId(parent.getUserId());
						deal3.setSubjectType("1");
						//deal3.setDealId(user.getUserAccount());
						deal3.setRemark(user.getUserAccount());
						moneyDealDao.save(deal3);
						
						PrizePay paybouns = new PrizePay();
						paybouns.setCreateDate(time);
						paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
						paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
						paybouns.setRelationUser(user.getUserAccount());
						paybouns.setUserId(parent.getUserAccount());
						paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
						prizePayDao.save(paybouns);
						
						//奖金累加到个人账户
						parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
						parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
						parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate))));
						parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate))));
						userAccountDao.update(parentAccount);
					}
				}else if("3".equals(parent.getLevel())){
					if(floor == 2 || floor ==4 || floor ==6 || floor ==8){
						deal = new MoneyDeal();
						deal.setAccountSubject(ShopConstants.POINT);
						deal.setAccountType(ShopConstants.PRIZE);
						deal.setCreateDate(time);
						deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						deal.setSubjectType("1");
						deal.setUserId(parent.getUserId());
						deal.setDealId(user.getUserAccount());
						deal.setRemark(user.getUserAccount());
						moneyDealDao.save(deal);	
						deal2 = new MoneyDeal();
						deal2.setAccountSubject(ShopConstants.RECONSUME);
						deal2.setAccountType(ShopConstants.RESUMER);
						deal2.setCreateDate(time);
						deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
						deal2.setSubjectType("1");
						deal2.setUserId(parent.getUserId());
						deal2.setRemark(user.getUserAccount());
						//deal.setDealId(user.getUserAccount());
						moneyDealDao.save(deal2);
						
						//爱心基金
						deal3 = new MoneyDeal();
						deal3.setAccountSubject(ShopConstants.LOVE);
						deal3.setAccountType(ShopConstants.LOVE_FUND);
						deal3.setCreateDate(time);
						deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
						deal3.setUserId(parent.getUserId());
						deal3.setSubjectType("1");
						//deal3.setDealId(user.getUserAccount());
						deal3.setRemark(user.getUserAccount());
						moneyDealDao.save(deal3);
						
						PrizePay paybouns = new PrizePay();
						paybouns.setCreateDate(time);
						paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
						paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
						paybouns.setRelationUser(user.getUserAccount());
						paybouns.setUserId(parent.getUserAccount());
						paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
						prizePayDao.save(paybouns);
						
						//奖金累加到个人账户
						parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
						parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
						parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate))));
						parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate))));
						userAccountDao.update(parentAccount);
					}
				}else if("4".equals(parent.getLevel())){
					if(floor == 2 || floor ==4 || floor ==6 || floor ==8 || floor ==10 || floor ==12){
						deal = new MoneyDeal();
						deal.setAccountSubject(ShopConstants.POINT);
						deal.setAccountType(ShopConstants.PRIZE);
						deal.setCreateDate(time);
						deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						deal.setSubjectType("1");
						deal.setUserId(parent.getUserId());
						deal.setDealId(user.getUserAccount());
						deal.setRemark(user.getUserAccount());
						moneyDealDao.save(deal);	
						deal2 = new MoneyDeal();
						deal2.setAccountSubject(ShopConstants.RECONSUME);
						deal2.setAccountType(ShopConstants.RESUMER);
						deal2.setCreateDate(time);
						deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
						deal2.setSubjectType("1");
						deal2.setUserId(parent.getUserId());
						deal2.setRemark(user.getUserAccount());
						//deal.setDealId(user.getUserAccount());
						moneyDealDao.save(deal2);
						//爱心基金
						deal3 = new MoneyDeal();
						deal3.setAccountSubject(ShopConstants.LOVE);
						deal3.setAccountType(ShopConstants.LOVE_FUND);
						deal3.setCreateDate(time);
						deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
						deal3.setUserId(parent.getUserId());
						deal3.setSubjectType("1");
						//deal3.setDealId(user.getUserAccount());
						deal3.setRemark(user.getUserAccount());
						moneyDealDao.save(deal3);
						
						PrizePay paybouns = new PrizePay();
						paybouns.setCreateDate(time);
						paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
						paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
						paybouns.setRelationUser(user.getUserAccount());
						paybouns.setUserId(parent.getUserAccount());
						paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
						prizePayDao.save(paybouns);
						
						//奖金累加到个人账户
						parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
						parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
						parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate)));
						parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate)));
						userAccountDao.update(parentAccount);
					}
				}else if("5".equals(parent.getLevel())){
					if(floor == 2 || floor ==4 || floor ==6 || floor ==8 || floor ==10 || floor ==12 || floor ==14 || floor ==16){
						deal = new MoneyDeal();
						deal.setAccountSubject(ShopConstants.POINT);
						deal.setAccountType(ShopConstants.PRIZE);
						deal.setCreateDate(time);
						deal.setIncome(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						deal.setSubjectType("1");
						deal.setUserId(parent.getUserId());
						deal.setDealId(user.getUserAccount());
						deal.setRemark(user.getUserAccount());
						moneyDealDao.save(deal);	
						deal2 = new MoneyDeal();
						deal2.setAccountSubject(ShopConstants.RECONSUME);
						deal2.setAccountType(ShopConstants.RESUMER);
						deal2.setCreateDate(time);
						deal2.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(consumeRate)));
						deal2.setSubjectType("1");
						deal2.setUserId(parent.getUserId());
						deal2.setRemark(user.getUserAccount());
						//deal.setDealId(user.getUserAccount());
						moneyDealDao.save(deal2);
						//爱心基金
						deal3 = new MoneyDeal();
						deal3.setAccountSubject(ShopConstants.LOVE);
						deal3.setAccountType(ShopConstants.LOVE_FUND);
						deal3.setCreateDate(time);
						deal3.setIncome(levelamount.multiply(new BigDecimal(0.01).multiply(loveRate)));
						deal3.setUserId(parent.getUserId());
						deal3.setSubjectType("1");
						//deal3.setDealId(user.getUserAccount());
						deal3.setRemark(user.getUserAccount());
						moneyDealDao.save(deal3);
						
						PrizePay paybouns = new PrizePay();
						paybouns.setCreateDate(time);
						paybouns.setPointPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate));
						paybouns.setConsumerPrize(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate));
						paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
						paybouns.setRelationUser(user.getUserAccount());
						paybouns.setUserId(parent.getUserAccount());
						paybouns.setLoveFund(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate));
						prizePayDao.save(paybouns);
						
						//奖金累加到个人账户
						parentAccount = userAccountDao.findByAccount(parent.getUserAccount());
						parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(prizeRate)));
						parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(consumeRate)));
						parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(levelamount.multiply(new BigDecimal(0.01)).multiply(loveRate)));
						userAccountDao.update(parentAccount);
					}
				}
			}
			
			//领导奖
			/*List<User> recommend = new ArrayList<User>();
			findreco(recommend,recommendUser);
			List<User> first = null;
			BigDecimal bonusprize = BigDecimal.ZERO;
			BigDecimal leadprize = BigDecimal.ZERO;
			for (User recom : recommend) {
				first = getRecommend(recom.getUserAccount());//所有都取一代对碰的10%
				if(first != null && first.size() > 0){
					for (User one : first) {
						bonusprize = moneyDealDao.findBonusByRelationId(one.getUserId(), user.getUserAccount());
						if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
							bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
							leadprize = bonusprize.multiply(new BigDecimal(0.1)); //领导奖
						}
					}
					
				}
				if("1".equals(recom.getLevel())){//一星  取一代的10%
					//取一代
				}else if("2".equals(recom.getLevel())){  //二星的  取一代的10% 二代的5%
					if(first != null && first.size() > 0){
						List<User> seconde = findNextChild(first);//取出二代
						for (User two : seconde) {
							bonusprize = moneyDealDao.findBonusByRelationId(two.getUserId(), user.getUserAccount());
							if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
								bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
								leadprize = leadprize.add(bonusprize.multiply(new BigDecimal(0.05))); //领导奖
							}
						}
					}
				}else if("3".equals(recom.getLevel())){  //三星的  取一代的10% 二代的10%
					List<User> seconde = findNextChild(first);//取出二代
					for (User two : seconde) {
						bonusprize = moneyDealDao.findBonusByRelationId(two.getUserId(), user.getUserAccount());
						if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
							bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
							leadprize = leadprize.add(bonusprize.multiply(new BigDecimal(0.1))); //领导奖
						}
					}
				}else if("4".equals(recom.getLevel())){  //四星的  取一代的10% 二代的10% 三代的5%
					List<User> seconde = findNextChild(first);//取出二代
					for (User two : seconde) {
						bonusprize = moneyDealDao.findBonusByRelationId(two.getUserId(), user.getUserAccount());
						if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
							bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
							leadprize = leadprize.add(bonusprize.multiply(new BigDecimal(0.1))); //领导奖
						}
					}
					List<User> third = findNextChild(seconde);
					for (User three : third) {
						bonusprize = moneyDealDao.findBonusByRelationId(three.getUserId(), user.getUserAccount());
						if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
							bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
							leadprize = leadprize.add(bonusprize.multiply(new BigDecimal(0.05))); //领导奖
						}
					}
				}else if("5".equals(recom.getLevel())){  //五星的  取一代的10% 二代的10%  三代的10%
					List<User> seconde = findNextChild(first);//取出二代
					for (User two : seconde) {
						bonusprize = moneyDealDao.findBonusByRelationId(two.getUserId(), user.getUserAccount());
						if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
							bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
							leadprize = leadprize.add(bonusprize.multiply(new BigDecimal(0.1))); //领导奖
						}
					}
					List<User> third = findNextChild(seconde);
					for (User three : third) {
						bonusprize = moneyDealDao.findBonusByRelationId(three.getUserId(), user.getUserAccount());
						if(bonusprize != null && bonusprize.compareTo(BigDecimal.ZERO)==1){
							bonusprize = bonusprize.divide(prizeRate,2,BigDecimal.ROUND_HALF_EVEN);
							leadprize = leadprize.add(bonusprize.multiply(new BigDecimal(0.1))); //领导奖
						}
					}
				}
				if(leadprize.compareTo(BigDecimal.ZERO) ==1){//大于0
					deal = new MoneyDeal();
					deal.setAccountSubject(ShopConstants.LEADER);
					deal.setAccountType(ShopConstants.PRIZE);
					deal.setCreateDate(time);
					deal.setIncome(leadprize.multiply(prizeRate));
					deal.setSubjectType("1");
					deal.setUserId(recom.getUserId());
					deal.setDealId(user.getUserAccount());
					deal.setRemark(user.getUserAccount());
					moneyDealDao.save(deal);	
					deal2 = new MoneyDeal();
					deal2.setAccountSubject(ShopConstants.RECONSUME);
					deal2.setAccountType(ShopConstants.RESUMER);
					deal2.setCreateDate(time);
					deal2.setIncome(leadprize.multiply(consumeRate));
					deal2.setSubjectType("1");
					deal2.setUserId(recom.getUserId());
					deal2.setRemark(user.getUserAccount());
					//deal.setDealId(user.getUserAccount());
					moneyDealDao.save(deal2);
					deal3 = new MoneyDeal();
					deal3.setAccountSubject(ShopConstants.LOVE);
					deal3.setAccountType(ShopConstants.LOVE_FUND);
					deal3.setCreateDate(time);
					deal3.setIncome(leadprize.multiply(loveRate));
					deal3.setUserId(recom.getUserId());
					deal3.setSubjectType("1");
					//deal3.setDealId(user.getUserAccount());
					deal3.setRemark(user.getUserAccount());
					moneyDealDao.save(deal3);
					
					PrizePay paybouns = new PrizePay();
					paybouns.setCreateDate(time);
					paybouns.setLeaderPrize(leadprize.multiply(prizeRate));
					paybouns.setConsumerPrize(leadprize.multiply(consumeRate));
					paybouns.setRegisterAmount(new BigDecimal(dicItem.getAttr1()));
					paybouns.setRelationUser(user.getUserAccount());
					paybouns.setUserId(recom.getUserAccount());
					paybouns.setLoveFund(leadprize.multiply(loveRate));
					prizePayDao.save(paybouns);
					//奖金累加到个人账户
					parentAccount = userAccountDao.findByAccount(recom.getUserAccount());
					parentAccount.setBonusMoney(parentAccount.getBonusMoney().add(leadprize.multiply(prizeRate)));
					parentAccount.setConsumerMoney(parentAccount.getConsumerMoney().add(leadprize.multiply(consumeRate)));
					parentAccount.setRecommendMoney(parentAccount.getRecommendMoney().add(leadprize.multiply(loveRate)));
					userAccountDao.update(parentAccount);
				}
			}*/
			
			/*//对碰奖也是
			if("1".equals(user.getLocation())){  //1:左区          2:右区
				fatherUser.setSparel(new Integer(fatherUser.getSparel() + 1).toString());
				fatherUser.setSparelAmount(fatherUser.getSparelAmount().add(new BigDecimal(dicItem.getAttr1())));
				fatherUser.setLeftLeave(leftLeave);
				fatherUser.setLeftLeaveAmount(leftLeaveAmount);
			}else{ // 2 右区
				fatherUser.setSparer(new Integer(fatherUser.getSparer() + 1).toString());
				fatherUser.setApaerAmount(fatherUser.getSparelAmount().add(new BigDecimal(dicItem.getAttr1())));
				
			}*/
			return 0;
	}

	private void findParent(List list, User user){
		User puser = getUserByAccount(user.getParentId());
		list.add(puser);
		if(puser.getParentId()!=null && !"".equals(puser.getParentId())){
			findParent(list, puser);
		}
	}
	
	private void findreco(List list, User user){
		if(user != null){
			User ruser = getUserByAccount(user.getRecommendId());
			if(ruser != null){
				list.add(ruser);
				if(ruser.getRecommendId()!=null && !"".equals(ruser.getRecommendId())){
					findreco(list, ruser);
				}							
			}
		}
	}

	public int getRecommendCnt(String account) {
		List<User> list =  userInfoDao.findByProperty("recommendId", account);
		if(list != null && list.size() > 0){
			return list.size();
		}                       
		return 0;
	}

	private List<User> findNextChild(List<User> list){
		List<User> childlist = new ArrayList<User>();
		for (User user : list) {
			List<User> childs =  userInfoDao.findByProperty("recommendId", user.getUserAccount());
			if(childs != null && childs.size() > 0){
				childlist.addAll(childs);
			}
		}
		return childlist;
	}

	public User findAdmin() {
		List<User> userList = userInfoDao.findByProperty("role", ShopConstants.ADMIN);
		if(userList != null && userList.size() > 0){
			User user = userList.get(0);
			return user;
		}
		return null;
	}

	private User findChildByLoc(User user, String location){
		User u = new User();
		u.setActivity(ShopConstants.TRUE);
		u.setParentId(user.getUserAccount());
		u.setLocation(location);
		List<User> list = userInfoDao.findByExample(u);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	public UserBank findAdminBank() {
		List<UserBank> list = userBankDao.findAdminBankByAccount();
		if(list != null && list.size() >0 ){
			return list.get(0);
		}
		return null;
	}


	public User getUserByAccount(String account) {
		List<User> list = userInfoDao.findByProperty("userAccount", account);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}


	public List<User> getRedirectChild(String account) {
		return userInfoDao.findByProperty("parentId", account, Order.asc("location"));

	}


	public UserBank findByAccount(String account) {
		List<UserBank> list = userBankDao.findByProperty("userId", account);
		if(list != null && list.size() >0 ){
			return list.get(0);
		}
		return null;
	}


	public void deleteUser(User user) {
		userInfoDao.delete(user);
	}

}
